Reputation: 45214
I'm showing dynamically-loaded images in NSMenuItem
s like the picture below.
I'm loading and resizing the image as:
[menuItem setImage:[self processImage: [[NSImage alloc] url]]];
[menu addItem:menuItem];
processImage
function resizes the downloaded 88x88 px. image to 24x24 px. (shown like below). This causes it to not to look like a retina pictures. But if I keep it at 48x48px it is not shown as small like 24x24, so the NSMenuItem doesn't actually do the resizing.
However I need to show these images in the same points of view as 48x48. to make it look like @2x. I couldn't find a way to use [NSMenuItem setImage:]
method to do that or without saving the contents of NSImage
to file as 48x48 and make the file name end with @2x
.
If I use the 48x48 image in setImage:
method, NSMenuItem's height just gets twice bigger and it really shows 48 points * 48 points.
Any ideas how can I achieve retina images in NSMenuItem without saving in memory images to file?
Upvotes: 0
Views: 780
Reputation: 90571
It should be sufficient to simply do [image setSize:NSMakeSize(24, 24)]
on an image that has more pixels than that. -setSize:
sets its logical size in points. The representations in the image determines how many pixels it has. If the pixel dimensions are greater than the point dimensions, then there's more detail. This detail can be used if the drawing destination is also high-resolution.
If that doesn't work, you can construct the image "manually" by initializing it at the desired size, creating a bitmap image representation with the available detail, set that rep's size and pixel dimensions (separately), and adding that representation to the image.
Upvotes: 5
Reputation: 52538
You could use the class method imageWithSize:flipped:drawingHandler:
This will call your drawingHandler to actually draw the image at the right resolution when it is needed. If you create the NSImage with a size of 24x24 then your drawingHandler will be called to draw 24x24 pixels before the image is copied onto a non-retina screen. It will be called to draw 48x48 pixels before the image is copied onto a retna screen.
drawingHandler would be called again if you move the window where the NSImage is drawn between a retina and a non-retina display.
Upvotes: 0
Reputation: 7758
I might be misunderstanding, but you can't expect to scale the image to 24x24 and have it magically display at retina 48x48. You're better off keeping the image 48x48 and scaling it down on non-retina screens automatically (just by virtue of setting the image size to 24x24)
Upvotes: 0