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?
Asked
Active
Viewed 1,344 times
1
-
The usual naming convention is that a 100x100 @2x image means it's 200 pixels by 200 pixels, not 50 pixels by 50 pixels. `NSImage`s can have multiple representations. An image's size is always in points, not pixels. The pixel dimensions of the representation(s) may differ from the image's size in points and may (probably, even) also differ from the other representations. So, there is not just one pixel size of an `NSImage`. – Ken Thomases Jan 05 '15 at 20:17
1 Answers
1
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.

canhazbits
- 1,664
- 1
- 14
- 19
-
1I'm trying to open an image with the @2x suffix. It is 1024x1024, however both heights you mention give me the same value of 512. – Nicolas Miari Oct 12 '16 at 03:58