+ (NSData *)compressImage:(UIImage *)image {
int width = (int)image.size.width;
int height = (int)image.size.height;
int updateWidth = width;
int updateHeight = height;
int longSide = MAX(width, height);
int shortSide = MIN(width, height);
float scale = ((float) shortSide / longSide);
// 大小压缩
if (shortSide < 1080 || longSide < 1080) {
// 如果宽高任何一边都小于 1080
updateWidth = width;
updateHeight = height;
} else { // 如果宽高都大于 1080
if (width < height) {
// 说明短边是宽
updateWidth = 1080;
updateHeight = 1080 / scale;
} else { // 说明短边是高
updateWidth = 1080 / scale;
updateHeight = 1080;
}
}
CGSize compressSize = CGSizeMake(updateWidth, updateHeight);
UIGraphicsBeginImageContext(compressSize);
[image drawInRect:CGRectMake(0,0, compressSize.width, compressSize.height)];
UIImage *compressImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// 质量压缩 50%
NSData *compressData = UIImageJPEGRepresentation(compressImage, 0.5);
return compressData;
}