Search code examples
imagemacosnsviewpreviewnsimage

How to display images as well as OS X Preview?


I am trying to display myPhoto.JPG in a custom view. I'm zooming by adjusting myBox in the following:

[myImage drawInRect:myBox fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];

Unfortunately, when I zoom in far enough to see individual pixels, the pixel edges are blurry. In contrast, when I open the same photo in Preview, the pixel edges are clean and crisp. (Also, zooming and panning is far more fluid in Preview.)

I'd greatly appreciate any thoughts on how to achieve Preview level quality in my custom view.


Solution

  • In drawRect: replace the line

    [myImage drawInRect:myBox fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
    

    with these lines:

    NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];
    NSImageInterpolation currentValue = [currentContext imageInterpolation];
    [currentContext setImageInterpolation:NSImageInterpolationNone];
        [myImage drawInRect:myBox fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
    [currentContext setImageInterpolation:currentValue];   // restore the old value
    

    (see the docs for setImageInterpolation: in NSGraphicsContext)