首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用NSURL访问保存的照片中的图像并上传到S3的正确方式?

使用NSURL访问保存的照片中的图像并上传到S3的正确方式?
EN

Stack Overflow用户
提问于 2014-09-09 02:59:19
回答 2查看 567关注 0票数 3

我正在使用此代码将图像上传到S3

代码语言:javascript
复制
    AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
    AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];
    uploadRequest.bucket = @"my-photo-bucket";
    uploadRequest.key = @"test_upload";
    long long fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:self.imageUrl.path error:nil][NSFileSize] longLongValue];
    uploadRequest.contentLength = [NSNumber numberWithUnsignedLongLong:fileSize];
    uploadRequest.body = self.imageUrl.absoluteURL;

    [[transferManager upload:uploadRequest] continueWithBlock:^id(BFTask *task) {
        NSLog(@"%@", task.error);
        return nil;
    }];

当我尝试上传文件时,它失败了,因为URL似乎没有指向我的应用程序可以访问的内容:

代码语言:javascript
复制
2014-09-08 11:57:47.014 myapp[1551:60b] Url: assets-library://asset/asset.JPG?id=721E68A8-DF94-4404-A37D-FECDCDC60C1D&ext=JPG, File Size: (null) 
2014-09-08 11:57:47.025 myapp[1551:60b] Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x17827c2c0 {NSFilePath=/asset.JPG, NSUnderlyingError=0x178256e60 "The operation couldn’t be completed. No such file or directory"} 

对我来说,这似乎是一个有效的URL。我做错了什么?

EN

回答 2

Stack Overflow用户

发布于 2014-12-31 08:47:01

看起来AWS-SDK-IOS项目目前不支持这一点。请参阅github bug report

他们建议的解决办法是在开始上传之前将资产复制到您的应用程序目录。

票数 1
EN

Stack Overflow用户

发布于 2015-04-26 20:37:04

这与我的工作完美,检查它。

代码语言:javascript
复制
//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;
                                                       }];
}

下面是如何使用NSURL从保存的照片中获取图像:

代码语言:javascript
复制
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-----");
     }];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25731267

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档