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

时间:2015-09-26 18:52:28

标签: ios objective-c amazon-web-services

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

1 个答案:

答案 0 :(得分:1)

您可以通过'继续'来更新用户界面。块。因此,如果您显示的图片是nil,则可以在代码段的这一部分进行更新:

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

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