【问题标题】:Downloading image form AWS S3 in Objective C在 Objective C 中从 AWS S3 下载图像
【发布时间】:2015-08-01 08:22:20
【问题描述】:

所以我正在尝试从 Amazon AWS S3 下载图片,并且我正在使用他们提供的模板代码,但它不是 wokring。

NSString *downloadFilePath = [NSTemporaryDirectory() stringByAppendingString:@"download-testImage.jpg"];
NSURL *downloadFileURL = [NSURL fileURLWithPath:downloadFilePath];
AWSS3TransferManagerDownloadRequest *downloadRequest = [AWSS3TransferManagerDownloadRequest new];

downloadRequest.bucket = @"tempBucketName"; //changed name for security reasons
downloadRequest.key = @"testImage.jpg";
downloadRequest.downloadingFileURL = downloadFileURL;
NSLog(@"About to start");
// Download the file.
[[self.transferManager download:downloadRequest] continueWithExecutor:[AWSExecutor mainThreadExecutor] withBlock:^id(AWSTask *task) {
    NSLog(@"Started task");
   if (task.error){
       NSLog(@"In if statement");
       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.
       NSLog(@"success");
   }
   return nil;
}];
cell.postImage.image = [UIImage imageWithContentsOfFile:downloadFilePath];
NSLog(@"%f", [UIImage imageWithContentsOfFile:downloadFilePath].size.width);

正在打印的唯一内容是“即将开始”“完成”,最后,其打印的图像尺寸为 0.0。我添加了那些 NSLogs 来帮助你们查明我的问题,我该如何解决这个问题?

【问题讨论】:

  • “完成”未出现在您的代码中。

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


【解决方案1】:

下载是异步的,因此您需要将其分派到主队列,即

if (task.result) {
   AWSS3TransferManagerDownloadOutput *downloadOutput = task.result;
   //File downloaded successfully.

 NSLog(@"success");

   dispatch_async(dispatch_get_main_queue(), ^{
            cell.postImage.image = [UIImageimageWithContentsOfFile:downloadFilePath];
NSLog(@"%f", [UIImage imageWithContentsOfFile:downloadFilePath].size.width);
        });

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 2016-07-27
    • 1970-01-01
    • 2021-01-19
    • 2019-05-09
    • 2021-04-02
    相关资源
    最近更新 更多