Cam*_*kew 3 amazon-s3 amazon-web-services ios
似乎这应该相对简单.我正在使用适用于iOS的AWS SDK(v2),我正在尝试下载.png文件并将其显示在UIImage中的屏幕上.一切都有效!非常奇怪......
这是我的代码:
AWSStaticCredentialsProvider *credentialsProvider = [AWSStaticCredentialsProvider credentialsWithAccessKey:@"MY_ACCESS_KEY" secretKey:@"MY_SECRET_KEY"];
AWSServiceConfiguration *configuration = [AWSServiceConfiguration configurationWithRegion:AWSRegionUSWest1 credentialsProvider:credentialsProvider];
[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;
AWSS3 *transferManager = [[AWSS3 alloc] initWithConfiguration:configuration];
AWSS3GetObjectRequest *getImageRequest = [AWSS3GetObjectRequest new];
getImageRequest.bucket = @"MY_BUCKET";
getImageRequest.key = @"MY_KEY";
[[transferManager getObject:getImageRequest] continueWithBlock:^id(BFTask *task) {
if(task.error)
{
NSLog(@"Error: %@",task.error);
}
else
{
NSLog(@"Got image");
NSData *data = [task.result body];
UIImage *image = [UIImage imageWithData:data];
myImageView.image = image;
}
return nil;
}];
Run Code Online (Sandbox Code Playgroud)
当执行此代码时,执行continueWithBlock,没有任务错误,因此记录了Got图像.这种情况发生得相当快.但是,直到大约10秒钟后,UIImageView才会在屏幕上更新.我甚至通过调试器来查看该行后面的任何NSLog(@"Got image");行是否需要很长时间,而不是.它们都执行得非常快,但UIImageView不会在UI上更新.
问题是您正在从后台线程更新UI组件.该continueWithBlock:块在后台线程中执行,并导致上述行为.您有两种选择:
在块中使用Grand Central Dispatch并在主线程上运行它:
...
NSURL *fileURL = [task.result body];
NSData *data = // convert fileURL to data
dispatch_async(dispatch_get_main_queue(), ^{
UIImage *image = [UIImage imageWithData:data];
myImageView.image = image;
});
...
Run Code Online (Sandbox Code Playgroud)用于mainThreadExecutor在主线程上运行块:
[[transferManager getObject:getImageRequest] continueWithExecutor:[BFExecutor mainThreadExecutor]
withBlock:^id(BFTask *task) {
...
Run Code Online (Sandbox Code Playgroud)希望这可以帮助,
| 归档时间: |
|
| 查看次数: |
2362 次 |
| 最近记录: |