+ (NSDictionary*)_compressedInfoBylubanAlgorithm:(NSData*)imageData calibrateSize:(int)calibrateSize{
CGSize imageSize = [[self class] getImageSizeWithData:imageData];
int width = imageSize.width;
int height = imageSize.height;
// 缩略图尺寸设为2的倍数,为后面的缩放做准备
int thumbW = width % 2 == 1 ? width + 1 : width;
int thumbH = height % 2 == 1 ? height + 1 : height;
// width = 短
// height = 长
BOOL exchanged = NO;
if (thumbW > thumbH) {
width = thumbH;
height = thumbW;
exchanged = YES;
}
double scale = ((double) width / height);
double size = 0;
if (scale <= 1 && scale > 0.5625) {
if (height < 1664) {
if (imageData.length / 1024 < 150) {
size = imageData.length/1024;
}
else{
size = (width * height) / pow(1664, 2) * 150;
size = size < 60 ? 60 : size;
}
} else if (height >= 1664 && height < 4990) {
thumbW = width / 2;
thumbH = height / 2;
size = (thumbW * thumbH) / pow(2495, 2) * 300;
size = size < 60 ? 60 : size;
} else if (height >= 4990 && height < 10240) {
thumbW = width / 4;
thumbH = height / 4;
size = (thumbW * thumbH) / pow(2560, 2) * 300;
size = size < 100 ? 100 : size;
} else {
int multiple = height / 1280 == 0 ? 1 : height / 1280;
thumbW = width / multiple;
thumbH = height / multiple;
size = (thumbW * thumbH) / pow(2560, 2) * 300;
size = size < 100 ? 100 : size;
}
} else if (scale <= 0.5625 && scale > 0.5) {
if (height < 1280 && imageData.length / 1024 < 200) {
size = imageData.length/1024;
}
else{
int multiple = height / 1280 == 0 ? 1 : height / 1280;
thumbW = width / multiple;
thumbH = height / multiple;
size = (thumbW * thumbH) / (1440.0 * 2560.0) * 200;
size = size < 100 ? 100 : size;
}
} else {
int multiple = (int) ceil(height / (1280.0 / scale));
thumbW = width / multiple;
thumbH = height / multiple;
size = ((thumbW * thumbH) / (1280.0 * (1280 / scale))) * 500;
size = size < 100 ? 100 : size;
}
if (exchanged) {
thumbW = thumbW + thumbH;
thumbH = thumbW - thumbH;
thumbW = thumbW - thumbH;
}
//不能超过服务端限制 1M
size = size * 1024;
size = MAX(size, calibrateSize);// size < calibrateSize(用于降低压缩的程度,提升压缩速度) < M1
size = MIN(size, imageData.length); //不超过初始大小
size = MIN(size, M1);
NSDictionary* compressedInfo = @{@"size":@(size),
@"scale":@(scale),
@"thumbW":@(thumbW),
@"thumbH":@(thumbH)};
return compressedInfo;
}