繁体   English   中英

等待直到AWSS3TransferManagerDownloadRequest完成(iOS,Objective-C)

[英]Waiting until AWSS3TransferManagerDownloadRequest is finished (iOS, Objective-C)

我正在制作一个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];}

您可以从“ continue with”块继续更新UI。 因此,如果要显示的图像为nil ,则可以在代码段的此部分中对其进行更新:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM