gpt4 book ai didi

ios - 如何在 iOS 中将图像上传到 AWS S3?

转载 作者:行者123 更新时间:2023-11-29 12:09:41 24 4
gpt4 key购买 nike

我正在使用 UIImagePicker 在我的应用中选择或拍摄照片。选择图像后,我需要将其上传到 AWS S3。我做了以下步骤,

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imgView.image = chosenImage;
imageUrl = info[UIImagePickerControllerReferenceURL];
NSString * timestamp = TimeStamp;
NSString *UUID = [[NSUUID UUID] UUIDString];

fileName = [NSString stringWithFormat:@"posts/images/%@%@.jpg", UUID, timestamp];

[picker dismissViewControllerAnimated:YES completion:NULL];

}

并且,对于上传,

AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];
uploadRequest.body = image;
uploadRequest.key = fileName;
uploadRequest.bucket = MY_BUCKET;
uploadRequest.ACL = AWSS3ObjectCannedACLPublicRead;
AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];

__weak BTEditProfileViewController *weakSelf = self;
[[transferManager upload:uploadRequest] continueWithBlock:^id(AWSTask *task) {
if (task.error) {
if ([task.error.domain isEqualToString:AWSS3TransferManagerErrorDomain]) {
switch (task.error.code) {
case AWSS3TransferManagerErrorCancelled:
case AWSS3TransferManagerErrorPaused:
{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"COMPLETED!!");
});
}
break;

default:
NSLog(@"Upload failed: [%@]", task.error);
break;
}
} else {
NSLog(@"Upload failed: [%@]", task.error);
}
}

if (task.result) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"RESULT:::%@",task.result);
});
}

return nil;
}];

当我运行它时,出现以下错误。

Upload failed: [Error Domain=com.amazonaws.AWSS3TransferManagerErrorDomain Code=5 "'body' can not be nil" UserInfo={NSLocalizedDescription='body' can not be nil}]

我该如何解决这个问题?

最佳答案

body 属性需要是 NSURL 的实例(需要是文件 URL)。

请注意,我们正在逐步淘汰 AWSS3TransferManagerAWSS3TransferUtility 替换 AWSS3TransferManager 并且可以 upload an NSData ,所以你应该看看。

关于ios - 如何在 iOS 中将图像上传到 AWS S3?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33848677/

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