我一直在瀏覽每一個堆棧溢出帖子和在線視頻,我找不到能正常工作的解決方案。現在,用戶在上傳到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;
}];
所以它看起來像它無論是複雜或不值得在所有上傳前將圖像壓縮。我想我會處理更大的圖像,然後在更好的緩存系統上工作,謝謝你的深刻反應! –