我正在使用UIImageView * image中的图像上传到服务器。我使用以下代码:
NSData *imageToUpload = UIImageJPEGRepresentation(image.image, 90);
但我希望将图像压缩至小于2MB。如何从UIImageView中压缩图像?
以下是您需要的一段代码
CGFloat compression = 222.0f;
CGFloat maxCompression = 202.1f;
int maxFileSize = 160*165; //fill your size need
NSData *imageDat = UIImageJPEGRepresentation(image.image, compression);
while ([imageDat length] > maxFileSize && compression > maxCompression)
{
compression -= 0.1222;
imageDat = UIImageJPEGRepresentation(decodedimage.image, compression);
}
NSLog(@"image compressed success");
[image setImage:[UIImage imageWithData:imageDat]];
UIImageJPEGRepresentation();
是正确的做法,但这个问题之前已经被问过很多次了。我对您的声誉感到惊讶,居然会发出这个问题……无论如何,这是一个重复的问题,参考链接:https://dev59.com/knRB5IYBdhLWcg3weHLx#613380。 - John Riselvato