gpt4 book ai didi

ios - 如何使用 AWS iOS SDK v2 将 UIImage 上传到 S3

转载 作者:IT王子 更新时间:2023-10-29 08:05:16 24 4
gpt4 key购买 nike

Github 中的 README 页面 ( https://github.com/aws/aws-sdk-ios-v2 ) 已经有一个从文件路径 URL 上传图片的示例:

AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];
uploadRequest.bucket = yourBucket;
uploadRequest.key = yourKey;
uploadRequest.body = yourDataURL; // <<<< this is a NSURL
uploadRequest.contentLength = [NSNumber numberWithUnsignedLongLong:fileSize];

但是,如果我在内存中只有一个 UIImage(没有文件路径)怎么办?是否可以使用 SDKUIImage(或其 NSData)上传到 S3

手动使用 HTTP API(使用 AFNetworking 之类的东西)会更容易吗?

最佳答案

虽然AWSiOSSDKv2不支持从内存上传图片,但您可以将其保存为文件,然后再上传。

//image you want to upload
UIImage* imageToUpload = [UIImage imageNamed:@"imagetoupload"];

//convert uiimage to
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", dateKey]];
[UIImagePNGRepresentation(imageToUpload) writeToFile:filePath atomically:YES];

NSURL* fileUrl = [NSURL fileURLWithPath:filePath];

//upload the image
AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];
uploadRequest.body = fileUrl;
uploadRequest.bucket = AWS_BUCKET_NAME;
uploadRequest.key = @"yourkey";
uploadRequest.contentType = @"image/png";
[[transferManager upload:thumbNailUploadRequest] continueWithExecutor:[BFExecutor mainThreadExecutor] withBlock:^id(BFTask *task) {
if(task.error == nil) {
NSLog(@"woot");
}
return nil;
}];

关于ios - 如何使用 AWS iOS SDK v2 将 UIImage 上传到 S3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25369602/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com