图片压缩处理

来源:互联网 发布:java 设计通用api接口 编辑:程序博客网 时间:2024/05/16 11:23

#import <UIKit/UIKit.h>


@interface UIImageTransformation : UIImage{

  

}


+ (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize;


+ (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize image:(UIImage*)image;


#pragma mark - image scale utility

+ (UIImage *)imageByScalingToMaxSize:(UIImage *)sourceImage;


+ (UIImage *)imageByScalingAndCroppingForSourceImage:(UIImage *)sourceImage targetSize:(CGSize)targetSize;


@end

#import "UIImageTransformation.h"

#import "DeviceManage.h"

@implementation UIImageTransformation


-(id)init{

    self = [super init];    

    return self;

}



+ (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize image:(UIImage*)image

{

    //    DLog(@"the image width is :%f", image.size.width);

    //    DLog(@"the image height is :%f", image.size.height);

    

    //UIImage 压缩后的图片

    UIImage *newImage = nil;

    

    //原图片大小

    CGSize imageSize = image.size;

    //原图片宽度

    CGFloat width = imageSize.width;

    //原图片高度

    CGFloat height = imageSize.height;

    

    

    //欲压缩宽度

    CGFloat targetWidth = targetSize.width;

    //欲压缩高度

    CGFloat targetHeight = targetSize.height;    

    //压缩比例系数(初始化为0

    CGFloat scaleFactor = 0.0;

    //图片的宽度和高度小于欲压缩的宽度和高度,不需要压缩

    if (width <= targetWidth && height <= targetHeight) {  

        return image;  

    }  

    //图片的宽度和高度都等于0,不需要压缩

    if (width == 0 || height == 0) {  

        

        return image;  

    } 

    

    //宽度比例

    CGFloat scaledWidth = targetWidth;

    //高度比例

    CGFloat scaledHeight = targetHeight;

    //压缩点

    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

    

    /*

     如果原图片大小跟欲压缩大小相同,则不执行压缩动作,否则执行压缩动作

     执行:

     */

    if ((CGSizeEqualToSize(imageSize, targetSize) == NO)&&(imageSize.width!=0)&&(imageSize.height!=0))

    {

        //宽度系数,格式 501000.5

        CGFloat widthFactor = targetWidth / width;

        //高度系数 格式 501000.5

        CGFloat heightFactor = targetHeight / height;

        

        

        if (widthFactor > heightFactor)

            scaleFactor = widthFactor; // scale to fit height

        else

            scaleFactor = heightFactor; // scale to fit width

        

        

        //压缩后的宽度=原宽度*宽度压缩系数

        scaledWidth  = width * scaleFactor;

        //压缩后的宽度=原高度*高度压缩系数

        scaledHeight = height * scaleFactor;

        

        // center the image

        if (widthFactor > heightFactor)

        {

            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;

        }

        else

            if (widthFactor < heightFactor)

            {

                thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;

            }

    }      

    //以下执行压缩动作

    UIGraphicsBeginImageContext(targetSize); // this will crop

    

    CGRect thumbnailRect = CGRectZero;

    thumbnailRect.origin = thumbnailPoint;

    thumbnailRect.size.width  = scaledWidth;

    thumbnailRect.size.height = scaledHeight;

    

    [image drawInRect:thumbnailRect];

    

    newImage = UIGraphicsGetImageFromCurrentImageContext();

    

    DLog(@"new  width is :%f", newImage.size.width);

    DLog(@"new height is :%f", newImage.size.height);

    if(newImage == nil)

    

    //pop the context to get back to the default

    UIGraphicsEndImageContext();

    return newImage;

}


#pragma mark - image scale utility


+ (UIImage *)imageByScalingToMaxSize:(UIImage *)sourceImage {

    //    if (sourceImage.size.width/2 < ScreenWidth)

    //        return sourceImage;

    

    CGFloat btWidth = 0.0f;

    CGFloat btHeight = 0.0f;

    CGFloat screenWidth = [[DeviceManage deviceManage] screenWidth];


    if (sourceImage.size.height/2 < screenWidth) {

        btHeight = screenWidth*2;

        btWidth = sourceImage.size.width * (screenWidth*2/sourceImage.size.height);

        //        btWidth = sourceImage.size.width * (ScreenWidth*2 / sourceImage.size.width);

        CGSize targetSize = CGSizeMake(btWidth, btHeight);

        return [self imageByScalingAndCroppingForSourceImage:sourceImage targetSize:targetSize];

    }

    

    if (sourceImage.size.width > sourceImage.size.height) {

        btHeight = screenWidth*2;

        btWidth = sourceImage.size.width * (screenWidth*2  / sourceImage.size.height);

    } else {

        btWidth = screenWidth*2 ;

        btHeight = sourceImage.size.height * (screenWidth*2  / sourceImage.size.width);

    }

    CGSize targetSize = CGSizeMake(btWidth, btHeight);

    return [self imageByScalingAndCroppingForSourceImage:sourceImage targetSize:targetSize];

}


+ (UIImage *)imageByScalingAndCroppingForSourceImage:(UIImage *)sourceImage targetSize:(CGSize)targetSize {

    UIImage *newImage = nil;

    

    CGSize imageSize = sourceImage.size;

    CGFloat width = imageSize.width;

    CGFloat height = imageSize.height;

    

    CGFloat targetWidth = targetSize.width;

    CGFloat targetHeight = targetSize.height;

    

    CGFloat scaleFactor = 0.0;

    CGFloat scaledWidth = targetWidth;

    CGFloat scaledHeight = targetHeight;

    

    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

    

    if (CGSizeEqualToSize(imageSize, targetSize) == NO)

    {

        CGFloat widthFactor = targetWidth / width;

        CGFloat heightFactor = targetHeight / height;

        

        if (widthFactor > heightFactor)

            scaleFactor = widthFactor; // scale to fit height

        else

            scaleFactor = heightFactor; // scale to fit width

        scaledWidth  = width * scaleFactor;

        scaledHeight = height * scaleFactor;

        

        // center the image

        if (widthFactor > heightFactor){

            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;

        }else if (widthFactor < heightFactor){

            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;

        }

    }

    UIGraphicsBeginImageContext(targetSize); // this will crop

    CGRect thumbnailRect = CGRectZero;

    thumbnailRect.origin = thumbnailPoint;

    thumbnailRect.size.width  = scaledWidth;

    thumbnailRect.size.height = scaledHeight;

    

    [sourceImage drawInRect:thumbnailRect];

    

    newImage = UIGraphicsGetImageFromCurrentImageContext();

    if(newImage == nil) NSLog(@"could not scale image");

    

    //pop the context to get back to the default

    UIGraphicsEndImageContext();

    return newImage;

}


//压缩图片

+ (UIImage*)imageWithImageSimple:(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;

}

@end



0 0
原创粉丝点击