Interpolated image

Imagine having a 10*10 NSImage you want to scale up in an NSImageView to like 100*100. The result will look pretty blurred. That’s caused by the image interpolation of NSImageView which is interpolating the image.

This might be fine for most things like photos, when scaling them down or up. But my situation were different, I wanted to zoom into it to visualise the pixels.

It took me many hours to find out that most proposed solutions like setting the magnificationFilter to kCAFilterNearest on the NSImageView’s layer are just not working. Maybe they do, somehow, I couldn’t make them work. If someone knows how, I would be glad to learn.

Not interpolated image

So back to the topic, once I figured out that interpolation was done by the NSImageView I simply implemented my own NSImageView and disabled the image interpolation on it’s current NSGraphicsContext.

import Cocoa

class CustomImageView: NSImageView {
    override func draw(_ dirtyRect: NSRect) {
        NSGraphicsContext.current!.imageInterpolation = NSImageInterpolation.none

For me it’s just fine, because I planned anyway to extend NSImageView for my needs. I hope this will save someone some time.