Question

I have a view that is being scaled down via a UIPinchGesture. A subview of that view is a UIImageView with an associate image. The issue is that the scale down seems to affect the quality of the image noticeably when it gets to about a 1/4 of the originally larger size.

I'm curious if there is a way to solve this without redrawing the image at the changed size as it is scaled down.

Was it helpful?

Solution

I would say no.

The best option you have to improve image visible quality in all states (without rescaling anything) would be to scale the original image to a size halfway in between your biggest and smallest sizes on screen. That would result in the least image scaling by the GPU.

Other than that, just rescale the image whenever you are applying a transform to the view by:

+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    //UIGraphicsBeginImageContext(newSize);
    // In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
    // Pass 1.0 to force exact pixel size.
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
    return newImage;
}

You may need to worry about aspect ratio, but that's another question entirely.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top