【发布时间】:2016-12-19 18:40:35
【问题描述】:
我一直在查看每个堆栈溢出帖子和在线视频,但找不到可以正常工作的解决方案。现在,用户在上传到 AWS S3 存储桶之前选择了我要压缩的照片。上传完美,但由于某种原因,压缩图像比原始图像大!例如,如果用户选择了一张 9KB 的照片,当我上传到 S3 时,这张照片是 28.5KB。我尝试了一张不同的照片,它是 48KB,在 S3 上“压缩”后是 378.9KB! (我正在使用最新的软件版本并使用模拟器进行编译)
我想在上传前尽可能压缩原始图像。
这是我目前所拥有的:
我如何“压缩”图像:
UIImage *compressedProductImage;
NSData *NSproductImage;
NSUInteger productImageSize;
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
self.productImage = info[UIImagePickerControllerOriginalImage];
[self.productImageImageView setImage:self.productImage];
[self dismissViewControllerAnimated:YES completion:nil];
NSproductImage = UIImageJPEGRepresentation(self.productImage, 0.5f);
productImageSize = [NSproductImage length];
compressedProductImage = [UIImage imageWithData: NSproductImage];
我如何上传照片:
//Convert product UIImage
NSArray *productImagePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *productImageFilePath = [[productImagePaths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@".png"]];
[UIImagePNGRepresentation(compressedProductImage) writeToFile:productImageFilePath atomically:YES];
NSURL *productImageFileUrl = [NSURL fileURLWithPath:productImageFilePath];
uploadRequest.body = productImageFileUrl;//Needs to be a NSURL
uploadRequest.bucket = AWS_BUCKET_NAME;
uploadRequest.key = productImageKey;
uploadRequest.contentType = @"image/png";
uploadRequest.ACL = AWSS3BucketCannedACLPublicRead;
[[transferManager upload:uploadRequest] continueWithExecutor:[AWSExecutor mainThreadExecutor] withBlock:^id(AWSTask *task) {
if (task.error != nil) {
NSLog(@"%s %@","Error uploading (product image):", uploadRequest.key);
}else{
NSLog(@"Product image upload completed");
}
return nil;
}];
【问题讨论】:
标签: ios objective-c amazon-s3 uiimage image-compression