博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

分两部分,1缩小图片大小2减弱图片质量
@1. 缩小图片大小
首先我们弄个catogory
//头文件
@interface UIImage (Compress)  
- (UIImage *)compressedImage;  
@end
//mm文件
#define MAX_IMAGEPIX 200.0          // max pix 200.0px  
#define MAX_IMAGEDATA_LEN 200.0   // max data length 5K  
@implementation UIImage (Compress)  
- (UIImage *)compressedImage {  
    CGSize imageSize = self.size;  
    CGFloat width = imageSize.width;  
    CGFloat height = imageSize.height;  
    if (width <= MAX_IMAGEPIX && height <= MAX_IMAGEPIX) {  
        // no need to compress.  
        return self;  
     
    if (width == 0 || height == 0) {  
        // void zero exception  
        return self;  
     
    UIImage *newImage = nil;  
    CGFloat widthFactor = MAX_IMAGEPIX / width;  
    CGFloat heightFactor = MAX_IMAGEPIX / height;  
    CGFloat scaleFactor = 0.0;  
    if (widthFactor > heightFactor)  
        scaleFactor = heightFactor; // scale to fit height  
    else  
        scaleFactor = widthFactor; // scale to fit width  
    CGFloat scaledWidth  = width * scaleFactor;  
    CGFloat scaledHeight = height * scaleFactor;  
    CGSize targetSize = CGSizeMake(scaledWidth, scaledHeight);  
    UIGraphicsBeginImageContext(targetSize); // this will crop  
    CGRect thumbnailRect = CGRectZero;  
    thumbnailRect.size.width  = scaledWidth;  
    thumbnailRect.size.height = scaledHeight;  
    [self drawInRect:thumbnailRect];  
    newImage = UIGraphicsGetImageFromCurrentImageContext();  
    //pop the context to get back to the default  
    UIGraphicsEndImageContext();  
    return newImage;  
}

 

封装函数为:

+ (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize

{

    // Create a graphics image context

    UIGraphicsBeginImageContext(newSize);

    

    // Tell the old image to draw in this new context, with the desired

    // new size

    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

    

    // Get the new image from the context

    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

    

    // End the context

    UIGraphicsEndImageContext();

    

    // Return the new image.

    return newImage;

}


@2.减弱图片质量
NSData *dataImg = UIImageJPEGRepresentation(img, 0.0001);

 

在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,并可根据自己的实际使用场景,设置压缩系数,进一步降低图片数据量大小

posted on 2012-11-30 23:23  Likwo  阅读(2781)  评论(1编辑  收藏  举报