兩種緊縮圖片的辦法:緊縮圖片質量(Quality),緊縮圖片尺寸(Size)。
緊縮圖片質量
NSData *data = UIImageJPEGRepresentation(image, compression); UIImage *resultImage = [UIImage imageWithData:data];
經過 UIImage 和 NSData 的互相轉化,減小 JPEG 圖片的質量來緊縮圖片。UIImageJPEGRepresentation:: 第二個參數 compression 取值 0.0~1.0,值越小表示圖片質量越低,圖片文件自然越小。
緊縮圖片尺寸
UIGraphicsBeginImageContext(size); [image draWinRect:CGRectMake(0, 0, size.width, size.height)]; resultImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
給定所需的圖片尺寸 size,resultImage 即為原圖 image 繪制為 size 大小的圖片。
緊縮圖片使圖片文件小於指定大小
假如對圖片明晰度要求不高,要求圖片的上傳、下載速度快的話,上傳圖片前需求緊縮圖片。緊縮到什麼水平要看詳細狀況,但普通會設定一個圖片文件最大值,例如 100 KB。可以用上訴兩種辦法來緊縮圖片。假定圖片轉化來的 NSData 對象為 data,經過data.length即可失掉圖片的字節大小。
緊縮圖片質量
比擬容易想到的辦法是,經過循環來逐步減小圖片質量,直到圖片稍小於指定大小(maxLength)。
+ (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; }
這樣循環次數多,效率低,耗時長。
可以經過二分法來優化。
+ (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; }
當圖片大小小於 maxLength,大於 maxLength * 0.9 時,不再持續緊縮。最多緊縮 6 次,1/(2^6) = 0.015625 < 0.02,也能到達每次循環 compression 減小 0.02 的效果。這樣的緊縮次數比循環減小 compression 少,耗時短。需求留意的是,當圖片質量低於一定水平時,持續緊縮沒無效果。也就是說,compression 持續減小,data 也不再持續減小。緊縮圖片質量的優點在於,盡能夠保存圖片明晰度,圖片不會分明模糊;缺陷在於,不能保證圖片緊縮後小於指定大小。
緊縮圖片尺寸
與之前相似,比擬容易想到的辦法是,經過循環逐步減小圖片尺寸,直到圖片稍小於指定大小(maxLength)。詳細代碼省略。異樣的問題是循環次數多,效率低,耗時長。可以用二分法來進步效率,詳細代碼省略。這裡引見另外一種辦法,比二分法更好,緊縮次數少,而且可以使圖片緊縮後剛好小於指定大小(不只是 < maxLength, > maxLength * 0.9)。
+ (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; }
[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; }
上面看下IOS圖片緊縮上傳的完成辦法
需求
很多時分我們上傳圖片常常遇到一些問題,要不就是圖片質質變差,要不就是圖片太大等等問題。這裡,我找到了一個算是目前比擬契合需求的處理方案。在原有根底上添加了靜態緊縮系數,改寫成Swift版本。
完成思緒
先調整分辨率,分辨率可以自己設定一個值,大於的就減少到這分辨率,小余的就堅持本來分辨率。然後再依據圖片最終大小來設置緊縮比,比方傳入maxSize = 30KB,最終計算大約這個大小的緊縮比。根本上最終出來的圖片數據依據以後分辨率能堅持差不多的大小同時不至於太模糊,跟微信,微博最終效果應該是差不多的,代碼依然有待優化!
完成代碼
Swift3.0之前舊版本緊縮形式
// MARK: - 降低質量 func resetSizeOfImageData(source_image: UIImage, maxSize: Int) -> NSData { //先調整分辨率 var newSize = CGSize(width: source_image.size.width, height: source_image.size.height) let tempHeight = newSize.height / 1024 let tempWidth = newSize.width / 1024 if tempWidth > 1.0 && tempWidth > tempHeight { newSize = CGSize(width: source_image.size.width / tempWidth, height: source_image.size.height / tempWidth) } else if tempHeight > 1.0 && tempWidth < tempHeight { newSize = CGSize(width: source_image.size.width / tempHeight, height: source_image.size.height / tempHeight) } UIGraphicsBeginImageContext(newSize) source_image.drawaspatternInRect(CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() //先判別以後質量能否滿足要求,不滿足再停止緊縮 var finallImageData = UIImageJPEGRepresentation(newImage,1.0) let sizeOrigin = Int64((finallImageData?.length)!) let sizeOriginKB = Int(sizeOrigin / 1024) if sizeOriginKB <= maxSize { return finallImageData! } //保管緊縮系數 let compressionQualityArr = NSMutableArray() let avg = CGFloat(1.0/250) var value = avg for var i = 250; i>=1; i-- { value = CGFloat(i)*avg compressionQualityArr.addObject(value) } //調整大小 //闡明:緊縮系數數組compressionQualityArr是從大到小存儲。 //思緒:折半計算,假如兩頭緊縮系數依然降不到目的值maxSize,則從後半局部開端尋覓緊縮系數;反之從前半局部尋覓緊縮系數 finallImageData = UIImageJPEGRepresentation(newImage, CGFloat(compressionQualityArr[125] as! NSNumber)) if Int(Int64((UIImageJPEGRepresentation(newImage, CGFloat(compressionQualityArr[125] as! NSNumber))?.length)!)/1024) > maxSize { //拿到最初的大小 finallImageData = UIImageJPEGRepresentation(newImage, 1.0) //從後半局部開端 for idx in 126..<250 { let value = compressionQualityArr[idx] let sizeOrigin = Int64((finallImageData?.length)!) let sizeOriginKB = Int(sizeOrigin / 1024) print("以後降到的質量:\(sizeOriginKB)") if sizeOriginKB > maxSize { print("\(idx)----\(value)") finallImageData = UIImageJPEGRepresentation(newImage, CGFloat(value as! NSNumber)) } else { break } } } else { //拿到最初的大小 finallImageData = UIImageJPEGRepresentation(newImage, 1.0) //從前半局部開端 for idx in 0..<125 { let value = compressionQualityArr[idx] let sizeOrigin = Int64((finallImageData?.length)!) let sizeOriginKB = Int(sizeOrigin / 1024) print("以後降到的質量:\(sizeOriginKB)") if sizeOriginKB > maxSize { print("\(idx)----\(value)") finallImageData = UIImageJPEGRepresentation(newImage, CGFloat(value as! NSNumber)) } else { break } } } return finallImageData! }
Swift3.0版本二分法緊縮形式
// MARK: - 降低質量 func resetSizeOfImageData(source_image: UIImage!, maxSize: Int) -> NSData { //先判別以後質量能否滿足要求,不滿足再停止緊縮 var finallImageData = UIImageJPEGRepresentation(source_image,1.0) let sizeOrigin = finallImageData?.count let sizeOriginKB = sizeOrigin! / 1024 if sizeOriginKB <= maxSize { return finallImageData! as NSData } //先調整分辨率 var defaultSize = CGSize(width: 1024, height: 1024) let newImage = self.newSizeImage(size: defaultSize, source_image: source_image) finallImageData = UIImageJPEGRepresentation(newImage,1.0); //保管緊縮系數 let compressionQualityArr = NSMutableArray() let avg = CGFloat(1.0/250) var value = avg var i = 250 repeat { i -= 1 value = CGFloat(i)*avg compressionQualityArr.add(value) } while i >= 1 /* 調整大小 闡明:緊縮系數數組compressionQualityArr是從大到小存儲。 */ //思緒:運用二分法搜索 finallImageData = self.halfFuntion(arr: compressionQualityArr.copy() as! [CGFloat], image: newImage, sourceData: finallImageData!, maxSize: maxSize) //假如還是未能緊縮到指定大小,則停止降分辨率 while finallImageData?.count == 0 { //每次降100分辨率 if defaultSize.width-100 <= 0 || defaultSize.height-100 <= 0 { break } defaultSize = CGSize(width: defaultSize.width-100, height: defaultSize.height-100) let image = self.newSizeImage(size: defaultSize, source_image: UIImage.init(data: UIImageJPEGRepresentation(newImage, compressionQualityArr.lastObject as! CGFloat)!)!) finallImageData = self.halfFuntion(arr: compressionQualityArr.copy() as! [CGFloat], image: image, sourceData: UIImageJPEGRepresentation(image,1.0)!, maxSize: maxSize) } return finallImageData! as NSData } // MARK: - 調整圖片分辨率/尺寸(等比例縮放) func newSizeImage(size: CGSize, source_image: UIImage) -> UIImage { var newSize = CGSize(width: source_image.size.width, height: source_image.size.height) let tempHeight = newSize.height / size.height let tempWidth = newSize.width / size.width if tempWidth > 1.0 && tempWidth > tempHeight { newSize = CGSize(width: source_image.size.width / tempWidth, height: source_image.size.height / tempWidth) } else if tempHeight > 1.0 && tempWidth < tempHeight { newSize = CGSize(width: source_image.size.width / tempHeight, height: source_image.size.height / tempHeight) } UIGraphicsBeginImageContext(newSize) source_image.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage! } // MARK: - 二分法 func halfFuntion(arr: [CGFloat], image: UIImage, sourceData finallImageData: Data, maxSize: Int) -> Data? { var tempFinallImageData = finallImageData var tempData = Data.init() var start = 0 var end = arr.count - 1 var index = 0 var difference = Int.max while start <= end { index = start + (end - start)/2 tempFinallImageData = UIImageJPEGRepresentation(image, arr[index])! let sizeOrigin = tempFinallImageData.count let sizeOriginKB = sizeOrigin / 1024 print("以後降到的質量:\(sizeOriginKB)\n\(index)----\(arr[index])") if sizeOriginKB > maxSize { start = index + 1 } else if sizeOriginKB < maxSize { if maxSize-sizeOriginKB < difference { difference = maxSize-sizeOriginKB tempData = tempFinallImageData } end = index - 1 } else { break } } return tempData }
以上所述是本站給大家引見的IOS完成圖片緊縮的兩種辦法,希望對大家有所協助,假如大家有任何疑問歡送給我留言,本站會及時回復大家的,在此也十分感激大家對本站網站的支持!
【iOS完成圖片緊縮的兩種辦法及圖片緊縮上傳功用】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!