Proper way to access image in saved photos using NSURL and upload to S3?

纵饮孤独 提交于 2019-12-05 22:06:11

It looks like this is currently not supported by the AWS-SDK-IOS project. See the github bug report.

Their suggested work around is to copy the assets to your app directory before initiating uploads.

This working perfectly with me, check it.

//Configuration 
AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc]
                                                      initWithRegionType:AWSRegionEUWest1
                                                      identityPoolId:CognitoIdentityPoolId];


AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:DefaultServiceRegionType
                                                                     credentialsProvider:credentialsProvider];

[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;

//Create temporary directory 
NSError *error = nil;
if (![[NSFileManager defaultManager] createDirectoryAtPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"upload"]
                               withIntermediateDirectories:YES
                                                attributes:nil
                                                     error:&error]) {
    NSLog(@"reading 'upload' directory failed: [%@]", error);
}

//write the image to the created directory 
UIImage *image =  [your image]; //Check below how do I get it 

NSString *fileName = [[[NSProcessInfo processInfo] globallyUniqueString] stringByAppendingString:@".jpg"];

NSString *filePath = [[NSTemporaryDirectory() stringByAppendingPathComponent:@"upload"] stringByAppendingPathComponent:fileName];

NSData * imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:filePath atomically:YES];

//Create upload request 
AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];
uploadRequest.body = [NSURL fileURLWithPath:filePath];

uploadRequest.key = [NSString stringWithFormat:@"%@", fileName]; //You can add you custom path here. Example: [NSString stringWithFormat:@"public/myImages/%@", fileName];


AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];

uploadRequest.bucket = S3BucketName;
uploadRequest.body = [NSURL fileURLWithPath:filePath];

[[transferManager upload:uploadRequest] continueWithExecutor:[BFExecutor mainThreadExecutor]
                                                       withBlock:^id(BFTask *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) {

                                                               // The file uploaded successfully.


                                                           }
                                                           return nil;
                                                       }];
}

And here is how to get the image form the saved photos using NSURL:

NSURL* imageURL = [NSURL URLWithString:urlString];

ALAssetsLibrary * _library = [[ALAssetsLibrary alloc] init];

[_library assetForURL:imageURL resultBlock:^(ALAsset *asset) {


    UIImage  *bigImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage] scale:0.5 orientation:UIImageOrientationUp];


    }
             failureBlock:^(NSError *error)
     {
         // error handling
         NSLog(@"failure-----");
     }];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!