Search code examples
retina-displaynsimage

NSImage's drawingHandler and retina screens


I know that when using the following snippet to create images and assign them to a view's layer's contents, the render block is automatically executed again when the view's backing properties changed, i.e. the window was moved to or from a retina display.

[NSImage imageWithSize:size flipped:NO drawingHandler:^BOOL(NSRect rect) {
    CGContextRef const context = NSGraphicsContext.currentContext.graphicsPort;
    ....
    return YES;
}];

Inside this rendering block, I am trying to vertically center another NSImage. I would like this to look as centered as possible on high resolution displays, but not blurry on non-retina screens. Hence I would like to know if there's any way to find out what contentScaleFactor the image is being rendered with.

This should really be straightforward, but even after hours of trying and searching the internet I have not found a way to find out whether the image is being rendered for a retina screen or not.


Solution

  • I figured it can be achieved via

    CGFloat const scale = CGBitmapContextGetWidth(context) / rect.size.width;

    However, I don't think this is a nice solution and there should/has to be something better.