圖片的壓縮有兩種方法
- 壓縮圖片質(zhì)量(Quality)(像素?cái)?shù)不變,長寬尺寸不變)
NSData *data = UIImageJPEGRepresentation(image, compression);
UIImage *resultImage = [UIImage imageWithData:data];
UIImageJPEGRepresentation:: 第二個(gè)參數(shù) compression 取值 0.0~1.0吟吝,值越小表示圖片質(zhì)量越低牵囤,圖片文件自然越小
適用于對(duì)圖片清晰度要求不高揭鳞,要求圖片的上傳、下載速度快野崇。
+ (UIImage *)compressImageQuality:(UIImage *)image toByte:(NSInteger)maxLength {
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];
return resultImage;
}
注意!!!
當(dāng)圖片質(zhì)量低于一定程度時(shí)乓梨,繼續(xù)壓縮沒有效果。
也就是說蕴侣,compression 繼續(xù)減小臭觉,data 也不再繼續(xù)減小。
優(yōu)點(diǎn):盡可能保留圖片清晰度狞膘,圖片不會(huì)明顯模糊什乙;
缺點(diǎn):不能保證圖片壓縮后小于指定大小
- 壓縮圖片尺寸(Size)(像素?cái)?shù)減少,長寬尺寸變懈ㄔ浮)
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
size為 指定的圖片尺寸 ,resultImage 為原圖 image 繪制成 size 大小后的圖片
+ (UIImage *)compressImageSize:(UIImage *)image toByte:(NSUInteger)maxLength {
UIImage *resultImage = image;
NSData *data = UIImageJPEGRepresentation(resultImage, 1);
NSUInteger lastDataLength = 0;
while (data.length > maxLength && data.length != lastDataLength) {
lastDataLength = data.length;
CGFloat ratio = (CGFloat)maxLength / data.length;
//每次繪制的尺寸 size鸽素,要把寬 width 和 高 height 轉(zhuǎn)換為整數(shù)亦鳞,防止繪制出的圖片有白邊
CGSize size = CGSizeMake((NSUInteger)(resultImage.size.width * sqrtf(ratio)),
(NSUInteger)(resultImage.size.height * sqrtf(ratio))); // Use NSUInteger to prevent white blank
UIGraphicsBeginImageContext(size);
// Use image to draw (drawInRect:), image is larger but more compression time
// Use result image to draw, image is smaller but less compression time
[resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
data = UIImageJPEGRepresentation(resultImage, 1);
}
return resultImage;
}
特點(diǎn):可以使圖片小于指定大小燕差,但會(huì)使圖片明顯模糊
兩種圖片壓縮方法結(jié)合
如果要保證圖片清晰度坝冕,建議選擇壓縮圖片質(zhì)量喂窟。如果要使圖片一定小于指定大小,壓縮圖片尺寸可以滿足磨澡。對(duì)于后一種需求稳摄,還可以先壓縮圖片質(zhì)量,如果已經(jīng)小于指定大小厦酬,就可得到清晰的圖片,否則再壓縮圖片尺寸昌讲。
+ (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;
}