zoukankan      html  css  js  c++  java
  • 图片压缩 浅析

    原文链接:http://www.jianshu.com/p/fb6a072fba47

    最近做的项目中需要上传头像,发表内容的时候也要涉及到图片上传,我直接用的原图上传,但是由于公司网络差,原图太大,老是加载好久好久,所以需要把原图裁剪或者修改分辨率之后再上传,找了好久,做了很多尝试才解决问题,下面记录一下,以便帮助自己查找,也希望能帮助其他人。

    我们一般图片上传最好使用UIImageJPEGRepresentation(image, 0.1)这个方法先把图片处理一下,其实这个方法把图片压缩之后,图片大小会小很多(小多少待会再讲),但是在app上人眼的感觉差不多,我对图片的处理如下:

    从相册拿到图片后,先经过方法修改图片的大小,也就是分辨率,方法如下:

    [objc] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. - (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)size  
    2. {  
    3.     UIGraphicsBeginImageContextWithOptions(size,NO,0.0);  
    4.     [image drawInRect:CGRectMake(0, 0, size.width, size.height)];  
    5.     UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();  
    6.     UIGraphicsEndImageContext();  
    7.     return scaledImage;  
    8. }  

    因为我是要上传头像,所以我把图片的size设置为(128,128),这里我在模拟器中截取到了一个320*320的图片,然后经过这个方法压缩后得到的图片是20.3KB,然后我在上传之前又用UIImageJPEGRepresentation(image, 0.05)把图片品质压缩了一下,图片的大小变成了1.5KB左右,这样的小图,在app上加载起来快急了,之前那个七八十KB的图片经常都加载不出来呢,现在OK了~。

    总结:1、用 - (UIImage *)scaleImage:(UIImage *)image toSize:(CGSize)size 修改图片的分辨率;

      2、用 UIImageJPEGRepresentation(image,0.1)这个方法来压缩图片品质。

    我们可以在不减小图片的分辨率(质量可以适当减小)的情况下,显著减小图片的大小

    float kCompressionQuality = 0.3; // 具体大小自己调
    NSData *photo = UIImageJPEGRepresentation(UIImage, kCompressionQuality);

    上面方法等价于下面: 压缩图片质量

    +(UIImage *)reduceImage:(UIImage *)image percent:(float)percent
    {
        NSData *imageData = UIImageJPEGRepresentation(image, percent);
        UIImage *newImage = [UIImage imageWithData:imageData];
        return newImage;
    }

    //将图片压缩到指定比例

    -(UIImage *)scaleToSize:(UIImage *)aImage size:(CGSize)size{
    
        //创建context,并将其设置为正在使用的context
        UIGraphicsBeginImageContext(size);
        //绘制出图片(大小已经改变)
        [aImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
        //获取改变大小之后的图片
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        //context出栈
        UIGraphicsEndImageContext();
        return newImage; //返回获得的图片
    }

    等比例压缩

    //等比例压缩
    -(UIImage *)oldImage:(UIImage *)oldImage toSize:(CGSize)size{
    
        UIImage *newImage = nil;//新照片对象
        CGSize theSize = oldImage.size;//压缩前图片size
    
        CGFloat width = theSize.width; //压缩前图片width
        CGFloat height = theSize.height;//压缩前图片height
    
        CGFloat newWidth = size.width; //压缩后图片width
        CGFloat newHeight = size.height;//压缩后图片height
    
        CGFloat scaleFactor = 0.0;//初值
    
        CGFloat toWidth = newWidth;//压缩后图片width
        CGFloat toHeight = newHeight;//压缩后图片height
    
        CGPoint thumnailPoint = CGPointMake(0.0, 0.0);//给初值
    
        if (CGSizeEqualToSize(theSize, size) == NO) {
            //判断是不是已经满足 theSize = size 要求
    
            CGFloat widthFac = newWidth/width;
            CGFloat heithrFac = newHeight/height;
            if (widthFac > heithrFac) {
                scaleFactor = widthFac;
            }else {
                scaleFactor = heithrFac;
            }
            //不满足做等比例缩小处理
            toWidth = width *scaleFactor;
            toHeight = height *scaleFactor;
    
            if (widthFac > heithrFac) {
                thumnailPoint.y = (newHeight - toHeight)* 0.5;
            }else if (widthFac < heithrFac){
                thumnailPoint.x = (newWidth - toWidth)* 0.5;
            }
        }
    
        //创建context,并将其设置为正在使用的context
        UIGraphicsBeginImageContext(size);
        CGRect thumbnailRect  = CGRectZero;
        thumbnailRect.origin = thumnailPoint;
        thumbnailRect.size.width = toWidth;
        thumbnailRect.size.height = toHeight;
    
        //绘制出图片(大小已经改变)
        [oldImage drawInRect:thumbnailRect];
    
        newImage = UIGraphicsGetImageFromCurrentImageContext();
    
        //结果判断
        if (newImage == nil) {
            [NSException exceptionWithName:@"提示" reason:@"Error:image scale fail" userInfo:nil];
        }
        UIGraphicsEndImageContext();
        return newImage;
    }
    
    
    -(UIImage *) imageCompressForWidth:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth{
    
        UIImage *newImage = nil;
    
        CGSize imageSize = sourceImage.size;
    
        CGFloat width = imageSize.width;
        CGFloat height = imageSize.height;
    
        CGFloat targetWidth = defineWidth;
        CGFloat targetHeight = height / (width / targetWidth);
        CGSize size = CGSizeMake(targetWidth, targetHeight);
        CGFloat scaleFactor = 0.0;
    
        CGFloat scaledWidth = targetWidth;
        CGFloat scaledHeight = targetHeight;
    
        CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
    
        if(CGSizeEqualToSize(imageSize, size) == NO){
            CGFloat widthFactor = targetWidth / width;
            CGFloat heightFactor = targetHeight / height;
            if(widthFactor > heightFactor){
                scaleFactor = widthFactor;
            }
            else{
                scaleFactor = heightFactor;
            }
    
            scaledWidth = width * scaleFactor;
            scaledHeight = height * scaleFactor;
            if(widthFactor > heightFactor){
                thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
            }else if(widthFactor < heightFactor){
                thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
            }
        }
    
        UIGraphicsBeginImageContext(size);
        CGRect thumbnailRect = CGRectZero;
        thumbnailRect.origin = thumbnailPoint;
        thumbnailRect.size.width = scaledWidth;
        thumbnailRect.size.height = scaledHeight;
    
        [sourceImage drawInRect:thumbnailRect];
    
        newImage = UIGraphicsGetImageFromCurrentImageContext();
        if(newImage == nil){
            NSLog(@"scale image fail");
        }
        UIGraphicsEndImageContext();
        return newImage;
    }

    图片的压缩其实是俩概念,
    1、是 “压” 文件体积变小,但是像素数不变,长宽尺寸不变,那么质量可能下降,
    2、是 “缩” 文件的尺寸变小,也就是像素数减少。长宽尺寸变小,文件体积同样会减小。
    这个 UIImageJPEGRepresentation(image, 0.0),是1的功能。
    这个 [sourceImage drawInRect:CGRectMake(0,0,targetWidth, targetHeight)] 是2的功能。
    所以,这俩你得结合使用来满足需求,不然你一味的用1,导致,图片模糊的不行,但是尺寸还是很大。

    我们还可以对图片进行部分截取

    -(UIImage*)getSubImage:(CGRect)rect
    {
        CGImageRefsubImageRef =CGImageCreateWithImageInRect(self.CGImage,rect);
        CGRectsmallBounds =CGRectMake(0,0, CGImageGetWidth(subImageRef),CGImageGetHeight(subImageRef));
    
        UIGraphicsBeginImageContext(smallBounds.size);
        CGContextRefcontext =UIGraphicsGetCurrentContext();
        CGContextDrawImage(context,smallBounds,subImageRef);
        UIImage*smallImage =[UIImage imageWithCGImage:subImageRef];
        UIGraphicsEndImageContext();
    
        returnsmallImage;
    }

    //--------------截取部分图片到指定位置-------------------------

    图片(UIImage*)img
    要截取的起始坐标sx:(int)sx1 sy:(int)sy1
    要截取的长度和宽度sw:(int)sw1 sh:(int)sh1
    最终要显示的坐标desx:(int)desx1 desy:(int)desy1
    
    -(UIImage*)objectiveDrawRegion:(UIImage*)img sx:(int)sx1 sy:(int)sy1sw:(int)sw1 sh:(int)sh1 desx:(int)desx1 desy:(int)desy1{
    [selfsaveImage:imgname:@&quot;objectiveDrawRegion1.png&quot;];
    
    //创建图片缓冲
    void*imageDataRegion=malloc(screenWidth*screenHeight*32);
    CGColorSpaceRefiColorSpaceRegion=CGColorSpaceCreateDeviceRGB();
        CGContextRefiDeviceRegion=CGBitmapContextCreate(imageDataRegion,screenWidth,screenHeight,8,4*screenWidth,iColorSpaceRegion,kCGImageAlphaPremultipliedLast);
    
    //剪切区域
        CGRectclipRegion=CGRectMake(sx1,sy1,sw1,sh1);
        CGContextClipToRect(iDeviceRegion,clipRegion);
    
        CGFloatwidthf=img.size.width;
        CGFloatheightf=img.size.height;
    
    CGRect  cg=CGRectMake(0.0,0.0, widthf, heightf);
    //画底图
        CGContextDrawImage(iDeviceRegion,cg,img.CGImage);
    
    //将缓冲形成图片
        CGImageRefioffRegion=CGBitmapContextCreateImage(iDeviceRegion);
    
    CGRect  cg1=CGRectMake(desx1,desy1, sw1, sh1);
    UIImage *ui=[UIImageimageWithCGImage:ioffRegion];
    
    CGContextDrawImage(当前context,cg1,ui.CGImage);
    
    //清除缓冲
       CGColorSpaceRelease(iColorSpaceRegion);
      CGContextRelease(iDeviceRegion);
       CGImageRelease(ioffRegion);
      free(imageDataRegion);
    //    iDeviceRegion=NULL;
    //    imageDataRegion=0;
    
    returnui;
    }



    参考:http://www.henishuo.com/ios-image-compressed/

  • 相关阅读:
    python3.7中asyncio的具体实现
    Nginx开启gzip压缩解决react打包文件过大
    使用Promise发送多个异步请求, 全部完成后再执行
    React 轮播图实现
    scrapy学习
    我的react+material-ui之路
    QQ小橙团队排表机器人使用方法
    P5569 [SDOI2008] 石子合并 解题报告
    NOIP/CSP 做题记录
    CF923E Perpetual Subtraction 解题报告
  • 原文地址:https://www.cnblogs.com/Jenaral/p/5563613.html
Copyright © 2011-2022 走看看