1
votes

How does one scale down a UIImage equally on both dimensions by half?

I have a UIImage that is double the size of the UIImageView and would like it to be the same size, without using any of the content modes for filling or scaling etc.

1

1 Answers

6
votes

Just make yourself a new image (expressed as a category method of 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;
}