Search code examples
macosnsimagensimageviewretina

Detecting Size of Retina NSImage


I'm dropping an image into an NSImageView and outputting the image's size. This works fine for standard images, but I want to output the size of retina images in their non-retina size, for example a 100x100 @2x image I want to output a size of 50x50. Apart from detecting the filename I can't figure out a way of doing this. Is there a way of detecting the scale of an image so I can manipulate the output size?


Solution

  • Just had to deal with this myself.

    You can do it like this:

    NSImage *image = [...];
    NSData* data = image.TIFFRepresentation;                
    NSBitmapImageRep* bitmap = [NSBitmapImageRep imageRepWithData:data];
    CGFloat actualHeight = bitmap.pixelsHigh;
    

    So then if you compare the image.size.height to the bitmap.pixelsHigh, you can tell if it's a Retina image you're dealing with.