iOS 图片比例压缩

来源:互联网 发布:淘宝主持怎么找商家 编辑:程序博客网 时间:2024/05/06 22:10

iOS自带的提供了一个API如下

[html] view plaincopy
  1. NSData *UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality);    

在Iphone上有两种读取图片数据的简单方法: UIImageJPEGRepresentation和UIImagePNGRepresentation. UIImageJPEGRepresentation函数需要两个参数:图片的引用和压缩系数.而UIImagePNGRepresentation只需要图片引用作为参数.通过在实际使用过程中,比较发现: UIImagePNGRepresentation(UIImage* image) 要比UIImageJPEGRepresentation(UIImage* image, 1.0) 返回的图片数据量大很多.譬如,同样是读取摄像头拍摄的同样景色的照片, UIImagePNGRepresentation()返回的数据量大小为199K ,而 UIImageJPEGRepresentation(UIImage* image, 1.0)返回的数据量大小只为140KB,比前者少了50多KB.如果对图片的清晰度要求不高,还可以通过设置 UIImageJPEGRepresentation函数的第二个参数,大幅度降低图片数据量.譬如,刚才拍摄的图片, 通过调用UIImageJPEGRepresentation(UIImage* image, 1.0)读取数据时,返回的数据大小为140KB,但更改压缩系数后,通过调用UIImageJPEGRepresentation(UIImage* image, 0.5)读取数据时,返回的数据大小只有11KB多,大大压缩了图片的数据量 ,而且从视角角度看,图片的质量并没有明显的降低.因此,在读取图片数据内容时,建议优先使用UIImageJPEGRepresentation,并可根据自己的实际使用场景,设置压缩系数,进一步降低图片数据量大小。


[html] view plaincopy
  1. UIImage *imageNew = [info objectForKey:@"UIImagePickerControllerOriginalImage"];  
  2. imageNew = [self imageWithImage:imageNew scaledToSize:CGSizeMake(100, 100)];  
  3. NSData *imageData = UIImageJPEGRepresentation(imageNew, 0.0001);  
  4.   
  5. m_selectImage = [UIImage imageWithData:imageData];  


.h具体code

 

[html] view plaincopy
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface UIImage (UIImageExt)  
  4.   
  5. - (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size;  
  6.   
  7. - (UIImage *)imageByScalingAndCroppingForSize:(CGSize)targetSize;  
  8. @end  
.m具体code

[html] view plaincopy
  1. #import "UIImageExt.h"  
  2.   
  3.   
  4. @implementation UIImage (UIImageExt)  
  5.   
  6. - (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{  
  7.     // 创建一个bitmap的context  
  8.     // 并把它设置成为当前正在使用的context  
  9.     UIGraphicsBeginImageContext(size);  
  10.     // 绘制改变大小的图片  
  11.     [img drawInRect:CGRectMake(0, 0, size.width, size.height)];  
  12.     // 从当前context中创建一个改变大小后的图片  
  13.     UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();  
  14.     // 使当前的context出堆栈  
  15.     UIGraphicsEndImageContext();  
  16.     // 返回新的改变大小后的图片  
  17.     return scaledImage;  
  18. }  
  19.   
  20.   
  21.   
  22. - (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize  
  23. {  
  24.     UIImage *sourceImage = self;  
  25.     UIImage *newImage = nil;  
  26.     CGSize imageSize = sourceImage.size;  
  27.     CGFloat width = imageSize.width;  
  28.     CGFloat height = imageSize.height;  
  29.     CGFloat targetWidth = targetSize.width;  
  30.     CGFloat targetHeight = targetSize.height;  
  31.     CGFloat scaleFactor = 0.0;  
  32.     CGFloat scaledWidth = targetWidth;  
  33.     CGFloat scaledHeight = targetHeight;  
  34.     CGPoint thumbnailPoint = CGPointMake(0.0,0.0);  
  35.       
  36.     if (CGSizeEqualToSize(imageSize, targetSize) == NO)  
  37.     {  
  38.         CGFloat widthFactor = targetWidth / width;  
  39.         CGFloat heightFactor = targetHeight / height;  
  40.           
  41.         if (widthFactor > heightFactor)  
  42.             scaleFactor = widthFactor; // scale to fit height  
  43.         else  
  44.             scaleFactor = heightFactor; // scale to fit width  
  45.         scaledWidth  = width * scaleFactor;  
  46.         scaledHeight = height * scaleFactor;  
  47.           
  48.         // center the image  
  49.         if (widthFactor > heightFactor)  
  50.         {  
  51.             thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;  
  52.         }  
  53.         else  
  54.             if (widthFactor < heightFactor)  
  55.             {  
  56.                 thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;  
  57.             }  
  58.     }  
  59.       
  60.     UIGraphicsBeginImageContext(targetSize); // this will crop  
  61.       
  62.     CGRect thumbnailRect = CGRectZero;  
  63.     thumbnailRect.origin = thumbnailPoint;  
  64.     thumbnailRect.size.width  = scaledWidth;  
  65.     thumbnailRect.size.height = scaledHeight;  
  66.       
  67.     [sourceImage drawInRect:thumbnailRect];  
  68.       
  69.     newImage = UIGraphicsGetImageFromCurrentImageContext();  
  70.     if(newImage == nil)  
  71.         NSLog(@"could not scale image");  
  72.       
  73.     //pop the context to get back to the default  
  74.     UIGraphicsEndImageContext();  
  75.     return newImage;  
  76. }  
  77.   
  78. @end  
0 0
原创粉丝点击