码迷,mamicode.com
首页 > 其他好文 > 详细

等比压缩图片的方法

时间:2015-06-24 18:04:17      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

- (UIImage*)imageEqualRatioScaledToSize:(CGSize)targetSize

{

    UIImage *sourceImage = self;

    UIImage *newImage = nil;

    CGSize imageSize = sourceImage.size;

    CGFloat srcWidth = imageSize.width;

    CGFloat srcHeight = imageSize.height;

    CGFloat targetWidth = targetSize.width;

    CGFloat targetHeight = targetSize.height;

    

    if (srcWidth <= targetWidth && srcHeight <= targetHeight) {

        // 如果源图已经小于缩放尺寸,则返回源图

        newImage = sourceImage;

    } else {

        // 如果源图长或宽大于缩放尺寸,则等比缩放

        double scale = (double)targetWidth/srcWidth < (double)targetHeight/srcHeight ? (double)targetWidth/srcWidth : (double)targetHeight/srcHeight;

        

        CGFloat scaleWidth = scale * srcWidth;

        CGFloat scaleHeight = scale * srcHeight;

        

        CGSize newSize = CGSizeMake(scaleWidth, scaleHeight);

        UIGraphicsBeginImageContext(newSize);

        

     

        CGRect thumbnailRect = CGRectMake(0, 0, scaleWidth, scaleHeight);

        [sourceImage drawInRect:thumbnailRect];

        newImage = UIGraphicsGetImageFromCurrentImageContext();

        

        if(newImage == nil)

            DLog(@"could not scale image");

        UIGraphicsEndImageContext();

    }

    return newImage;

}

等比压缩图片的方法

标签:

原文地址:http://www.cnblogs.com/iphoneDevelop/p/4597908.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!