如何创建从AWS下载的图像数组?

时间:2017-02-09 18:49:44

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

场景:我在同一个游戏中拥有一个包含多个用户的游戏。每个用户都可以获得积分。当轮到我时,我希望他们的图像显示在imageView中。

每个用户都可以选择添加自己的图像(保存到AWS),但如果用户没有图像,则会为其分配默认图像。

问题:我能够成功创建一系列图片,但我有两个问题:

  1. 在完成下载块之前,为存储了图像的用户分配默认图像。下载完成后,其图像将更改为其存储的图像。
  2. 当用户添加新图片时,他们的图片永远不会更新。尽管AWS仅具有更新的图像,但它显示了之前添加的图像。
  3. 代码:

    -(void)getUserImagesFromServer {
    
    self.userPlayerImages = [NSMutableArray array];
    
    for (NSString *userId in self.playerUserIds) {
    
        // Construct the NSURL for the download location.
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:userId];
        NSURL *downloadingFileURL = [NSURL fileURLWithPath:filePath];
    
        // Construct the download request.
        AWSS3TransferManagerDownloadRequest *downloadRequest = [AWSS3TransferManagerDownloadRequest new];
        downloadRequest.bucket = @"boxscore-user-pictures";
        downloadRequest.key = userId;
        downloadRequest.downloadingFileURL = downloadingFileURL;
    
        [self downloadRequest:downloadRequest:^(BOOL finished) {
            if(finished){
    
                if (self.tempResultCount == 1) {
                    [self.userPlayerImages addObject: [UIImage imageWithContentsOfFile:filePath]];
                } else {
                    [self.userPlayerImages addObject:self.tempPlayerImage];
                }
            }
        }];
    }
    }
    
    -(void) downloadRequest:(AWSS3TransferManagerDownloadRequest *)downloadRequest :(myCompletion) compblock{
    
    AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
    
    __block int taskResult;
    // Download the file.
    [[transferManager download:downloadRequest] continueWithExecutor:[AWSExecutor mainThreadExecutor]
                                                           withBlock:^id(AWSTask *task) {
                                                               if (task.error){
    
                                                                   NSLog(@"Error: %@", task.error);
    taskResult = 0;
    
                                                               } else {
                                                                   NSLog(@"Successfully downloaded Image!");
    taskResult = 1;
    
                                                               }
                                                               self.tempResultCount = taskResult;
    
                                                               return nil;
                                                           }];
    
    
    compblock(YES);
    }
    

    视觉参考用户界面:

    Image1

    更新

    以下是用于将图像保存到AWS的代码:

    -(void)saveImageToServer:(UIImage*)image {
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",[self.sud objectForKey:@"userId"]]];
    [UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES];
    
    NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
    
    AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];
    uploadRequest.bucket = @"boxscore-user-pictures";
    uploadRequest.key = [NSString stringWithFormat:@"%@",[self.sud objectForKey:@"userId"]];
    uploadRequest.body = fileUrl;
    
    AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
    
    [[transferManager upload:uploadRequest] 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) {
                                                               AWSS3TransferManagerUploadOutput *uploadOutput = task.result;
                                                               // The file uploaded successfully.
                                                           }
                                                           return nil;
                                                       }];
    
    }
    

0 个答案:

没有答案