![](/img/trans.png)
[英]AWS S3 File Download with Objective-C Successful Then Unsuccessful
[英]Downloading image form AWS S3 in Objective C
因此,我嘗試從Amazon AWS S3下載圖片,並使用它們提供的模板代碼,但無法正常工作。
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以幫助你們查明我的問題,我該如何解決?
下載是異步的,因此您需要將其分派到主隊列,即
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);
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.