// // UIImage+MT.m // ADuTechnology // // Created by Simon on 2019/9/28. // Copyright © 2019 Simon. All rights reserved. // #import "UIImage+MT.h" @implementation UIImage (MT) //压缩图片 - (NSData *)compressQualityWithMaxLength:(NSInteger)maxLength { CGFloat compression = 1; NSData *data = UIImageJPEGRepresentation(self, compression); if (data.length < maxLength) return data; CGFloat max = 1; CGFloat min = 0; for (int i = 0; i < 12; ++i) { compression = (max + min) / 2; data = UIImageJPEGRepresentation(self, compression); if (data.length < maxLength * 0.9) { min = compression; } else if (data.length > maxLength) { max = compression; } else { break; } } return data; } @end