码迷,mamicode.com
首页 > 移动开发 > 详细

IOS图片压缩

时间:2016-09-27 19:35:37      阅读:464      评论:0      收藏:0      [点我收藏+]

标签:

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 height
22         else
23             scaleFactor = heightFactor; // scale to fit width
24         scaledWidth= width * scaleFactor;
25         scaledHeight = height * scaleFactor;
26         
27         // center the image
28         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 crop
39     
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 default
52     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图片压缩

标签:

原文地址:http://www.cnblogs.com/heyode/p/5913803.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!