gpt4 book ai didi

ios - 等待 AWSS3TransferManagerDownloadRequest 完成(iOS、Objective-C)

转载 作者:行者123 更新时间:2023-11-29 12:13:05 25 4
gpt4 key购买 nike

我正在制作一个 iPhone 应用程序并使用 S3 存储图像,现在我想下载它们并在我的应用程序中显示它们。问题是我在显示图像之前执行了 AWSS3TransferManagerDownloadRequest。问题来了:第一次想显示图片,显示不出来,估计是下载请求还没有完成。之后,每当我重新运行我的项目时,图像都会正常显示,大概是因为它已经存储在本地了。那么我怎样才能让图像立即显示出来。这是相关的功能。

- (void)makeImageWithBucket:(NSString *)bucket withKey:(NSString *)key atLocation:(NSString *)location{
AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];

// Construct the NSURL for the download location.
NSString *downloadingFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:location];
NSURL *downloadingFileURL = [NSURL fileURLWithPath:downloadingFilePath];

// Construct the download request.
AWSS3TransferManagerDownloadRequest *downloadRequest = [AWSS3TransferManagerDownloadRequest new];

downloadRequest.bucket = bucket;
downloadRequest.key = key;
downloadRequest.downloadingFileURL = downloadingFileURL;

// Download the file.
[[transferManager download:downloadRequest] continueWithExecutor:[AWSExecutor mainThreadExecutor]
withBlock:^id(AWSTask *task) {
if (task.error){
if ([task.error.domain isEqualToString:AWSS3TransferManagerErrorDomain]) {
switch (task.error.code) {
case AWSS3TransferManagerErrorCancelled:
case AWSS3TransferManagerErrorPaused:
break;

default:
NSLog(@"Error: %@", task.error);
break;
}
} else {
// Unknown error.
NSLog(@"Error: %@", task.error);
}
}

if (task.result) {

AWSS3TransferManagerDownloadOutput *downloadOutput = task.result;

//File downloaded successfully.
}
return nil;
}];

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 375, 667)];
imageView.image = [UIImage imageWithContentsOfFile:downloadingFilePath];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame: self.view.frame];
scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
scrollView.contentSize = CGSizeMake(375,imageView.frame.size.height);
[scrollView addSubview:imageView];
[self.view addSubview:scrollView];}

最佳答案

您可以从“继续” block 更新 UI。因此,如果您显示的图像是 nil,您可以在代码片段的这一部分更新它:

if (task.result) {
AWSS3TransferManagerDownloadOutput *downloadOutput = task.result;

//File downloaded successfully.
//Display the downloaded image here.
}

关于ios - 等待 AWSS3TransferManagerDownloadRequest 完成(iOS、Objective-C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32800808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com