iOS 对图片进行压缩

来源:互联网 发布:安卓飞机大战源码 编辑:程序博客网 时间:2024/06/10 14:59

https://segmentfault.com/q/1010000000701850

- (UIImage *)compressImage:(UIImage *)image toMaxFileSize:(NSInteger)maxFileSize {    CGFloat compression = 0.9f;    CGFloat maxCompression = 0.1f;    NSData *imageData = UIImageJPEGRepresentation(image, compression);    while ([imageData length] > maxFileSize && compression > maxCompression) {        compression -= 0.1;        imageData = UIImageJPEGRepresentation(image, compression);    }        UIImage *compressedImage = [UIImage imageWithData:imageData];    return compressedImage;}

0 0
原创粉丝点击