美文网首页
压缩图片

压缩图片

作者: NateLam | 来源:发表于2016-08-16 11:05 被阅读28次

    压缩图片有两种, 一种是换尺寸, 一种是压缩质量

    如果是jpeg可以直接

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

    上面等价于下面, 适用于格式为png的

    +(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;
    }
    

    对图片进行部分截取

    -(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:@"objectiveDrawRegion1.png"];

    //创建图片缓冲
    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.jianshu.com/p/fb6a072fba47?utm_campaign=hugo&utm_medium=reader_share&utm_content=note&utm_source=qq

    相关文章

      网友评论

          本文标题:压缩图片

          本文链接:https://www.haomeiwen.com/subject/harpsttx.html