gpt4 book ai didi

ios - 压缩从设备中获取或从库中选择的图像,iOS

转载 作者:可可西里 更新时间:2023-11-01 05:15:04 25 4
gpt4 key购买 nike

在我的应用程序中,允许用户从设备拍摄照片或从图库上传照片以将其设置为个人资料照片。现在当用户完成后,我需要将该图像上传到服务器。通常从设备获取的图像大小为 5-6MB。在上传到服务器之前,我需要将它压缩到 25KB。所以我正在使用以下方法

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
[picker release];
profilePicImageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSData* pngData = UIImagePNGRepresentation(profilePicImageView.image);
NSLog(@"image size of png is %d", [pngData length]);
NSData *imageData = UIImageJPEGRepresentation(profilePicImageView.image, 0.00001);
NSLog(@"image size is %d", [imageData length]);
}

这种方法的问题是,无论我将缩放参数设置得多么小,压缩后的文件大小都不会低于 ~230KB,这比我需要的高出近 10 倍。

上述方法有什么问题?我不能将图像压缩到 25KB 吗?

任何帮助,建议将不胜感激。

最佳答案

这是一个替代答案,我认为它更简洁。它来自堆栈溢出的另一个答案,但已经在我的代码中有一段时间了。我得到 ~15kB 大小的图像,这是从使用 4s 相机拍摄的标准图像(2.5k x 3.5k 像素)调整到更有用的分辨率:(400 x 600)。然后压缩UIImageJPEGRepresentation(pic,0.1)。

CGRect rect = CGRectMake(0,0,capturedImage.size.width/6,capturedImage.size.height/6);
UIGraphicsBeginImageContext( rect.size );
[capturedImage drawInRect:rect];
UIImage *picture1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSData *imageDataForResize = UIImageJPEGRepresentation(picture1,0.1);
UIImage *img=[UIImage imageWithData:imageDataForResize];
capturedImage = img; // reassigning to original image as to prevent changing things later in the code

谢谢!我希望这可以帮助其他人!当您尝试执行从服务器加载图像的快速 Collection View 加载时,这个问题很重要。

关于ios - 压缩从设备中获取或从库中选择的图像,iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9972454/

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