標籤:
iPhone拍出來的照片一般大小在1-10M,我們在上傳照片時,不可能上傳如此大的圖片到伺服器,一般我們會對照片進行壓縮。
常用的做法是,使用這個函數對圖片壓縮 UIImageJPEGRepresentation(UIImage * __nonnull image, CGFloat compressionQuality); 但這個函數有一個臨界值,不能對圖片無限制壓縮,一般到當壓縮比傳入0.1已達到臨界值了。這時如果我想把5M的圖片壓縮到50K是行不通的。
如果圖太大,我們要求壓縮的比例太大就不能使用以上的方法來壓縮,這時就要先對圖片裁剪,裁剪後再使用這個函數壓縮,這樣才能達到想要的大小。為了不使圖片變形,裁剪時要按原圖的寬高比來裁剪。可以寫成UIImage的分類方法代碼如下:
1 - (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize 2 { 3 UIImage *sourceImage = self; 4 UIImage *newImage = nil; 5 CGSize imageSize = sourceImage.size; 6 CGFloat width = imageSize.width; 7 CGFloat height = imageSize.height; 8 CGFloat targetWidth = targetSize.width; 9 CGFloat targetHeight = targetSize.height;10 CGFloat scaleFactor = 0.0;11 CGFloat scaledWidth = targetWidth;12 CGFloat scaledHeight = targetHeight;13 CGPoint thumbnailPoint = CGPointMake(0.0,0.0);14 15 if (CGSizeEqualToSize(imageSize, targetSize) == NO)16 {17 CGFloat widthFactor = targetWidth / width;18 CGFloat heightFactor = targetHeight / height;19 20 if (widthFactor > heightFactor)21 scaleFactor = widthFactor; // scale to fit height22 else23 scaleFactor = heightFactor; // scale to fit width24 scaledWidth= width * scaleFactor;25 scaledHeight = height * scaleFactor;26 27 // center the image28 if (widthFactor > heightFactor)29 {30 thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;31 }32 else if (widthFactor < heightFactor)33 {34 thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;35 }36 }37 38 UIGraphicsBeginImageContext(targetSize); // this will crop39 40 CGRect thumbnailRect = CGRectZero;41 thumbnailRect.origin = thumbnailPoint;42 thumbnailRect.size.width= scaledWidth;43 thumbnailRect.size.height = scaledHeight;44 45 [sourceImage drawInRect:thumbnailRect];46 47 newImage = UIGraphicsGetImageFromCurrentImageContext();48 if(newImage == nil)49 NSLog(@"could not scale image");50 51 //pop the context to get back to the default52 UIGraphicsEndImageContext();53 return newImage;54 }
然後給系統工具類寫一個壓縮圖片的類方法,根據不同大小的圖片採用不同大小的裁剪比和壓縮比,將壓縮後的圖片保證在50K左右的大小。
1 + (NSData *)compressWithOrgImg:(UIImage *)img 2 { 3 4 NSData *imageData = UIImageJPEGRepresentation(img, 1); 5 float length = imageData.length; 6 length = length/1024; 7 NSLog(@"壓縮前的大小:%fKB",length); 8 // 裁剪比例 9 CGFloat cout = 0.5;10 11 // 壓縮比例12 CGFloat imgCout = 0.1;13 if(length > 25000){ // 25M以上的圖片14 cout = 0.1;15 imgCout = 0;16 }else if(length > 10000){ // 10M以上的圖片17 cout = 0.2;18 imgCout = 0;19 }else if (length > 5000) { // 5M以上的圖片20 cout = 0.3;21 imgCout = 0;22 }else if (length > 1500) { // 如果原圖大於1.5M就換一個壓縮層級23 cout = 0.7;24 imgCout = 0.1;25 }else if (length > 1000) {26 cout = 0.8;27 imgCout = 0.2;28 }else if (length > 500) {29 cout = 0.8;30 imgCout = 0.3;31 }else{32 cout = 0.9;33 imgCout = 50/length;34 }35 36 37 // 按裁剪比例裁剪38 UIImage *compressImage = [img imageByScalingAndCroppingForSize:CGSizeMake(img.size.width * cout, img.size.height *cout)];39 40 41 // 那壓縮比例壓縮42 imageData = UIImageJPEGRepresentation(compressImage, imgCout);43 44 length= imageData.length / 1024;45 NSLog(@"裁剪比例:%f,壓縮比例:%f,壓縮後的大小:%fKB",cout,imgCout,length);46 return imageData;47 }
IOS圖片壓縮