缩小UIImage

时间:2012-08-10 21:28:00

标签: ios uiimage

如何在两个维度上将UIImage平均缩小一半?

我有一个UIImage,它的大小是UIImageView的两倍,并希望它的大小相同,而不使用任何内容模式进行填充或缩放等。

1 个答案:

答案 0 :(得分:6)

让自己成为一个新形象(表示为UIImage的类别方法):

- (UIImage*)scaleToSize:(CGSize)size {
    UIGraphicsBeginImageContext(size);

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0.0, size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, size.width, size.height), self.CGImage);

    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return scaledImage;
}