作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用来自 UIImageView * 图像的图像上传到服务器。我使用类似的代码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]];
希望这对您有帮助!!!
关于ios - 如何从 UIImageView 压缩图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14949132/
我是一名优秀的程序员,十分优秀!