更多圖片處理方法見圖片組件 BBWebImagehtml
兩種壓縮圖片的方法:壓縮圖片質量(Quality),壓縮圖片尺寸(Size)。ios
NSData *data = UIImageJPEGRepresentation(image, compression); UIImage *resultImage = [UIImage imageWithData:data];
經過 UIImage 和 NSData 的相互轉化,減少 JPEG 圖片的質量來壓縮圖片。UIImageJPEGRepresentation:: 第二個參數 compression 取值 0.0~1.0,值越小表示圖片質量越低,圖片文件天然越小。git
UIGraphicsBeginImageContext(size); [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; resultImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
給定所需的圖片尺寸 size,resultImage 即爲原圖 image 繪製爲 size 大小的圖片。github
若是對圖片清晰度要求不高,要求圖片的上傳、下載速度快的話,上傳圖片前須要壓縮圖片。壓縮到什麼程度要看具體狀況,但通常會設定一個圖片文件最大值,例如 100 KB。能夠用上訴兩種方法來壓縮圖片。假設圖片轉化來的 NSData 對象爲 data,經過data.length
便可獲得圖片的字節大小。swift
比較容易想到的方法是,經過循環來逐漸減少圖片質量,直到圖片稍小於指定大小(maxLength)。less
+ (UIImage *)compressImageQuality:(UIImage *)image toByte:(NSInteger)maxLength { CGFloat compression = 1; NSData *data = UIImageJPEGRepresentation(image, compression); while (data.length > maxLength && compression > 0) { compression -= 0.02; data = UIImageJPEGRepresentation(image, compression); // When compression less than a value, this code dose not work } UIImage *resultImage = [UIImage imageWithData:data]; return resultImage; }
這樣循環次數多,效率低,耗時長。優化
能夠經過二分法來優化。this
+ (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; }
static func compressImageQuality(_ image: UIImage, toByte maxLength: Int) -> UIImage { var compression: CGFloat = 1 guard var data = UIImageJPEGRepresentation(image, compression), data.count > maxLength else { return image } 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 } } return UIImage(data: data)! }
當圖片大小小於 maxLength,大於 maxLength * 0.9 時,再也不繼續壓縮。最多壓縮 6 次,1/(2^6) = 0.015625 < 0.02,也能達到每次循環 compression 減少 0.02 的效果。這樣的壓縮次數比循環減少 compression 少,耗時短。須要注意的是,當圖片質量低於必定程度時,繼續壓縮沒有效果。也就是說,compression 繼續減少,data 也再也不繼續減少。壓縮圖片質量的優勢在於,儘量保留圖片清晰度,圖片不會明顯模糊;缺點在於,不能保證圖片壓縮後小於指定大小。code
與以前相似,比較容易想到的方法是,經過循環逐漸減少圖片尺寸,直到圖片稍小於指定大小(maxLength)。具體代碼省略。一樣的問題是循環次數多,效率低,耗時長。能夠用二分法來提升效率,具體代碼省略。這裏介紹另一種方法,比二分法更好,壓縮次數少,並且可使圖片壓縮後恰好小於指定大小(不僅是 < maxLength, > maxLength * 0.9)。htm
+ (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; 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; }
static func compressImageSize(_ image: UIImage, toByte maxLength: Int) -> UIImage { guard var data = UIImageJPEGRepresentation(image, 1) else { return image } var resultImage: UIImage = image 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, 1)! } return resultImage }
[resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
是用新圖 resultImage 繪製,也能夠用原圖 image 來繪製。用原圖繪製,壓縮後圖片更接近指定大小,可是壓縮次數較多,耗時較長。一張大小爲 6064 KB 的圖片,壓縮圖片尺寸,原圖繪製與新圖繪製結果以下
指定大小(KB) | 原圖繪製壓縮後大小(KB) | 原圖繪製壓縮次數 | 新圖繪製壓縮後大小(KB) | 新圖繪製壓縮次數 |
---|---|---|---|---|
500 | 498 | 6 | 498 | 3 |
300 | 299 | 4 | 296 | 3 |
100 | 99 | 5 | 98 | 3 |
50 | 49 | 6 | 48 | 3 |
兩種繪製方法壓縮後大小很接近,與指定大小也很接近,但原圖繪製壓縮次數可達到新圖繪製壓縮次數的兩倍。建議使用新圖繪製,減小壓縮次數。壓縮後圖片明顯比壓縮質量模糊。
須要注意的是繪製尺寸的代碼CGSize size = CGSizeMake((NSUInteger)(resultImage.size.width * sqrtf(ratio)), (NSUInteger)(resultImage.size.height * sqrtf(ratio)));
,每次繪製的尺寸 size,要把寬 width 和 高 height 轉換爲整數,防止繪製出的圖片有白邊。
壓縮圖片尺寸可使圖片小於指定大小,但會使圖片明顯模糊(比壓縮圖片質量模糊)。
若是要保證圖片清晰度,建議選擇壓縮圖片質量。若是要使圖片必定小於指定大小,壓縮圖片尺寸能夠知足。對於後一種需求,還能夠先壓縮圖片質量,若是已經小於指定大小,就可獲得清晰的圖片,不然再壓縮圖片尺寸。
+ (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; }
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 }
Swift 代碼已上傳 GitHub:https://github.com/Silence-GitHub/CompressImageDemo
轉載請註明出處:http://www.cnblogs.com/silence-cnblogs/p/6346729.html