Search code examples
iosimageantialiasing

How do I turn off all the image processing to see actual, unmodified pixels when I zoom way in?


I have an app that needs to zoom far enough into images that I can clearly see the individual pixels on an image. I need to see clear squares of one color, with no anti-aliasing or other of the usually-helpful techniques for making images look good on a display. How do I stifle all this help? Here is code that works, but zooms into blurry, adjusted pixels:

- (void)drawRect:(CGRect)rect {
    CGRect photoRect = CGRectMake(currentTarget.origin.x,
                                  currentTarget.origin.y,
                                  currentTarget.size.width,
                                  currentTarget.size.height);
    CGContextRef context = UIGraphicsGetCurrentContext() ;
    CGContextSaveGState(context);
    CGImageRef subImageRef = 
        CGImageCreateWithImageInRect(sourceImage.CGImage, photoRect);
    UIImage *subImage = [UIImage imageWithCGImage:subImageRef];
    [subImage drawInRect:rect];
    CGImageRelease(subImageRef);
    CGContextRestoreGState(context);
    CGContextFlush(context);
}

Solution

  • Setting the interpolation quality for the context should help resolve this issue, although if you're allowing the user to zoom beyond the 1:1 pixel ratio, things will still look somewhat soft.

    CGContextSetInterpolationQuality(context, kCGInterpolationNone)
    

    In this instance, I've used kCGInterpolationNone which is effectively "nearest neighbour".

    For more information see Apple's reference documentation.