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

标签 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];}

最佳答案

您可以从“继续” 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/

相关文章:

iphone - 可达性中的 UIAlertView 异常

ios - UIView背景图片不显示

ios - 在 iOS 上使用 VoiceOver 时如何获取当前聚焦的元素?

objective-c - 对象 PromiseKit : Add new promises from within a promise

java - 如何使用 AWS CDK for Java 为账户创建 IAM 委托(delegate)人?

objective-c - UIPopoverController 内存崩溃

ios - 使用自动布局修复自定义 UITableViewCell

ios - 如何在CartoDB map 层中应用CSS?

postgresql - 经常更新的最佳缓存策略数据(Redis/Memcached vs Nginx/Varnish vs Materialized view)

amazon-web-services - 将内联策略添加到 aws SAM 模板