iOS 图片按比例压缩,指定大小压缩

使用系统方法UIImageJPEGRepresentation(UIimage *image,CGFloat quality)进行图片质量压缩,暂且叫参数quality为压缩比吧,取值范围为0~1。

此压缩并非线性,当quality为0.99时,大致压缩到原图片大小的1/3以内,也就是说你无法通过此方法来把一个图片压缩到原大小的一半。

弄明白这这一点剩下的就好办了,至于实现,根据自己的需去要DIY就行了~

首先声明,以下方法无法实现对图片大小的精确压缩。

 

1 //以40K大小为例,误差1K为例 
2   UIImage * image = [UIImage imageWithData:[self compressImageWithImage:image aimWidth:200 aimLength:40*1024 accuracyOfLength:1024]];

1、质量压缩方法实现:

 1 /**
 2  *  压缩图片质量,返回值为可直接转化成UIImage对象的NSData对象
 3  *  aimLength: 目标大小,单位:字节(b)
 4  *  accuracyOfLength: 压缩控制误差范围(+ / -),本方法虽然给出了误差范围,但实际上很难确定一张图片是否能压缩到误差范围内,无法实现精确压缩。
 5  */
 6 - (NSData *)compressImageWithImage:(UIImage *)image aimWidth:(CGFloat)width aimLength:(NSInteger)length accuracyOfLength:(NSInteger)accuracy{
 7     UIImage * newImage = [self imageWithImage:image scaledToSize:CGSizeMake(width, width * image.size.height / image.size.width)];
 8    
 9     NSData  * data = UIImageJPEGRepresentation(newImage, 1);
10     NSInteger imageDataLen = [data length];
11     
12     if (imageDataLen <= length + accuracy) {
13         return data;
14     }else{
15         NSData * imageData = UIImageJPEGRepresentation( newImage, 0.99);
16         if (imageData.length < length + accuracy) {
17             return imageData;
18         }
19         
20         CGFloat maxQuality = 1.0;
21         CGFloat minQuality = 0.0;
22         int flag = 0;
23         
24         while (1) {
25             CGFloat midQuality = (maxQuality + minQuality)/2;
26             
27             if (flag == 6) {
28                 NSLog(@"************* %ld ******** %f *************",UIImageJPEGRepresentation(newImage, minQuality).length,minQuality);
29                 return UIImageJPEGRepresentation(newImage, minQuality);
30             }
31             flag ++;
32             
33             NSData * imageData = UIImageJPEGRepresentation(newImage, midQuality);
34             NSInteger len = imageData.length;
35             
36             if (len > length+accuracy) {
37                 NSLog(@"-----%d------%f------%ld-----",flag,midQuality,len);
38                 maxQuality = midQuality;
39                 continue;
40             }else if (len < length-accuracy){
41                  NSLog(@"-----%d------%f------%ld-----",flag,midQuality,len);
42                 minQuality = midQuality;
43                 continue;
44             }else{
45                  NSLog(@"-----%d------%f------%ld--end",flag,midQuality,len);
46                 return imageData;
47                 break;
48             }
49         }
50     }
51 }
52 

 

2、压缩尺寸,传入带压缩图片对象以及目标大小即可实现。

 1  //对图片尺寸进行压缩--
 2 -(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
 3 {
 4     UIGraphicsBeginImageContext(newSize);
 5     [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
 6     UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
 7     UIGraphicsEndImageContext();
 8     return newImage;
 9 }

 

你可能感兴趣的:(iOS 图片按比例压缩,指定大小压缩)