我一直在浏览每一个堆栈溢出帖子和在线视频,我找不到能正常工作的解决方案。现在,用户在上传到AWS S3存储桶之前选择要压缩的照片。上传完美,但由于某种原因,压缩图像比原始图像大!例如,如果用户选择9KB照片,当我上传到S3时,照片是28.5KB。我尝试了一张不同的照片,它的大小是48KB,在S3上“压缩”它的378.9KB! (我用的一切最新的软件版本,并与模拟器编译)如何在iOS中压缩图像?
我要压缩的原始图像,就像我可以在上传之前。
这是我到目前为止有:
如何我 “压缩” 的形象:
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;
}];
所以它看起来像它无论是复杂或不值得在所有上传前将图像压缩。我想我会处理更大的图像,然后在更好的缓存系统上工作,谢谢你的深刻反应! –