我们都知道微信分享下缩略图的大小是有限制的(不超过32kb),不然地话就会出现点击分享无法调出微信的情况,为此我们必须对分享的图片进行压缩。
常规的图片压缩,有两种方法:压缩质量和压缩尺寸。
压缩质量
NSData *data = UIImageJPEGRepresentation(image, compression);
UIImage *resultImage = [UIImage imageWithData:data];
压缩尺寸
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
这两种方法各有什么区别呢?
压缩图片质量的优点在于,尽可能保留图片清晰度,图片不会明显模糊,缺点在于当图片质量低于一定程度时,继续压缩没有效果,所以不能确保压缩后的大小;
压缩图片尺寸可以使图片小于指定大小,但会使图片明显模糊(比压缩图片质量模糊)。
为此,针对微信缩略图的情况,我们最终需要图片大小小于32kb,但是我们同样希望保持图片质量,那么就需要两种方法进行结合。首先循环压缩质量,如果大小仍然大于32kb,再对图片尺寸进行压缩,直到接近32kb。
这里有童鞋写出了详细的压缩方法和技术细节(戳这里),并给出了相关代码,经实地测试,代码可用
oc代码:
+ (UIImage *)compressImage:(UIImage *)image toByte:(NSUInteger)maxLength {
// Compress by quality
CGFloat compression = 1;
NSData *data = UIImageJPEGRepresentation(image, compression);
if (data.length < maxLength) return image;
CGFloat max = 1;
CGFloat min = 0;
for (int i = 0; i < 6; ++i) {
compression = (max + min) / 2;
data = UIImageJPEGRepresentation(image, compression);
if (data.length < maxLength * 0.9) {
min = compression;
} else if (data.length > maxLength) {
max = compression;
} else {
break;
}
}
UIImage *resultImage = [UIImage imageWithData:data];
if (data.length < maxLength) return resultImage;
// Compress by size
NSUInteger lastDataLength = 0;
while (data.length > maxLength && data.length != lastDataLength) {
lastDataLength = data.length;
CGFloat ratio = (CGFloat)maxLength / data.length;
CGSize size = CGSizeMake((NSUInteger)(resultImage.size.width * sqrtf(ratio)),
(NSUInteger)(resultImage.size.height * sqrtf(ratio))); // Use NSUInteger to prevent white blank
UIGraphicsBeginImageContext(size);
[resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
data = UIImageJPEGRepresentation(resultImage, compression);
}
return resultImage;
}
swift代码:
static func compressImage(_ image: UIImage, toByte maxLength: Int) -> UIImage {
var compression: CGFloat = 1
guard var data = UIImageJPEGRepresentation(image, compression),
data.count > maxLength else { return image }
// Compress by size
var max: CGFloat = 1
var min: CGFloat = 0
for _ in 0..<6 {
compression = (max + min) / 2
data = UIImageJPEGRepresentation(image, compression)!
if CGFloat(data.count) < CGFloat(maxLength) * 0.9 {
min = compression
} else if data.count > maxLength {
max = compression
} else {
break
}
}
var resultImage: UIImage = UIImage(data: data)!
if data.count < maxLength { return resultImage }
// Compress by size
var lastDataLength: Int = 0
while data.count > maxLength, data.count != lastDataLength {
lastDataLength = data.count
let ratio: CGFloat = CGFloat(maxLength) / CGFloat(data.count)
let size: CGSize = CGSize(width: Int(resultImage.size.width * sqrt(ratio)),
height: Int(resultImage.size.height * sqrt(ratio)))
UIGraphicsBeginImageContext(size)
resultImage.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
resultImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
data = UIImageJPEGRepresentation(resultImage, compression)!
}
return resultImage
}