1

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.

1 Answer 1

2

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.

6
  • This is what I'm doing, though I hoped there was a better solution. Thanks.
    – Jordan
    Jan 24, 2014 at 18:38
  • A better solution as for what? UI smoothness, memory management, etc...?
    – Putz1103
    Jan 24, 2014 at 19:04
  • A solution where I don't have to redraw the image multiple times on the pinch down. Just redrawing once when I am pinching from a full screen view to a tiny thumbnail can potentially be pixelated. I had to have various thresholds for redrawing.
    – Jordan
    Jan 24, 2014 at 19:15
  • You could make it a tiling view with multiple scales. Then the "redrawing" would be completely transparent to the user (ideally).
    – Putz1103
    Jan 24, 2014 at 19:16
  • 1
    Yes, but the code is a bit confusing. I'm sure Apple has an example for a tiled image with multiple levels of detail. Here: developer.apple.com/library/ios/samplecode/photoscroller/…
    – Putz1103
    Jan 24, 2014 at 19:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.