图片压缩处理

@interface UIImage (fixOrientation) 

- (UIImage *)fixOrientation; 

@end

@implementation UIImage (fixOrientation)- (UIImage *)fixOrientation { // No-op if the orientation is already correct if (self.imageOrientation == UIImageOrientationUp) return self; // We need to calculate the proper transformation to make the image upright. // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored. CGAffineTransform transform = CGAffineTransformIdentity; switch (self.imageOrientation) { case UIImageOrientationDown: case UIImageOrientationDownMirrored: transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height); transform = CGAffineTransformRotate(transform, M_PI); break; case UIImageOrientationLeft: case UIImageOrientationLeftMirrored: transform = CGAffineTransformTranslate(transform, self.size.width, 0); transform = CGAffineTransformRotate(transform, M_PI_2); break; case UIImageOrientationRight: case UIImageOrientationRightMirrored: transform = CGAffineTransformTranslate(transform, 0, self.size.height); transform = CGAffineTransformRotate(transform, -M_PI_2); break; case UIImageOrientationUp: case UIImageOrientationUpMirrored: break; } switch (self.imageOrientation) { case UIImageOrientationUpMirrored: case UIImageOrientationDownMirrored: transform = CGAffineTransformTranslate(transform, self.size.width, 0); transform = CGAffineTransformScale(transform, -1, 1); break; case UIImageOrientationLeftMirrored: case UIImageOrientationRightMirrored: transform = CGAffineTransformTranslate(transform, self.size.height, 0); transform = CGAffineTransformScale(transform, -1, 1); break; case UIImageOrientationUp: case UIImageOrientationDown: case UIImageOrientationLeft: case UIImageOrientationRight: break; } // Now we draw the underlying CGImage into a new context, applying the transform // calculated above. CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height, CGImageGetBitsPerComponent(self.CGImage), 0, CGImageGetColorSpace(self.CGImage), CGImageGetBitmapInfo(self.CGImage)); CGContextConcatCTM(ctx, transform); switch (self.imageOrientation) { case UIImageOrientationLeft: case UIImageOrientationLeftMirrored: case UIImageOrientationRight: case UIImageOrientationRightMirrored: // Grr... CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage); break; default: CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage); break; } // And now we just create a new UIImage from the drawing context CGImageRef cgimg = CGBitmapContextCreateImage(ctx); UIImage *img = [UIImage imageWithCGImage:cgimg]; CGContextRelease(ctx); CGImageRelease(cgimg); return img;}@end



+(UIImage*)zipNSDataWithImage:(UIImage*)sourceImage

{

    //进行图像尺寸的压缩

    CGSizeimageSize = sourceImage.size;//取出要压缩的image尺寸

    CGFloatwidth = imageSize.width;    //图片宽度

    CGFloatheight = imageSize.height;  //图片高度

    //1.宽高大于1280(宽高比不按照2来算,按照1来算)

    if(width>1280||height>1280) {

        if(width>height) {

            CGFloatscale = height/width;

            width =1280;

            height = width*scale;

        }else{

            CGFloatscale = width/height;

            height =1280;

            width = height*scale;

        }

        //2.宽大于1280高小于1280

    }elseif(width>1280||height<1280){

        CGFloatscale = height/width;

        width =1280;

        height = width*scale;

        //3.宽小于1280高大于1280

    }elseif(width<1280||height>1280){

        CGFloatscale = width/height;

        height =1280;

        width = height*scale;

        //4.宽高都小于1280

    }else{

    }

    UIGraphicsBeginImageContext(CGSizeMake(width, height));

    [sourceImagedrawInRect:CGRectMake(0,0,width,height)];

    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();


    //进行图像的画面质量压缩

    NSData *data=UIImageJPEGRepresentation(newImage, 1.0);

    if(data.length>100*1024) {

        if(data.length>1024*1024) {//1M以及以上

            data=UIImageJPEGRepresentation(newImage,0.7);

        }elseif(data.length>512*1024) {//0.5M-1M

            data=UIImageJPEGRepresentation(newImage,0.8);

        }elseif(data.length>200*1024) {

            //0.25M-0.5M

            data=UIImageJPEGRepresentation(newImage,0.9);

        }

    }

    return[UIImageimageWithData:data];

}

你可能感兴趣的:(图片压缩处理)