IOS 图片按比例缩小

来源:互联网 发布:东莞天助网络怎么样 编辑:程序博客网 时间:2024/05/18 03:10

/**

 *  保持原来的长宽比,生成一个缩略图

 *

 *  @param image 要调整的图片

 *  @param asize 目标尺寸

 *

 *  @return UIImage

 */

- (UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSize)asize

{

    CGFloat w = image.self.size.width;

    CGFloat h = image.self.size.height;

    

    //默认图片宽度大于图片高度

    CGFloat percentage = asize.width / w;

    

    if (h < w) {

        percentage = asize.height / h;

    }

    

    w = w * percentage;

    h = h * percentage;

    

    UIImage *newimage;

    

    UIGraphicsBeginImageContext(asize);

    CGContextRef context =UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [[UIColorclearColor] CGColor]);

    UIRectFill(CGRectMake(0,0, asize.width, asize.height));//clear background

    [image drawInRect:CGRectMake(0,0, w, h)];

    newimage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    

    return newimage;

}


0 0
原创粉丝点击