Frage

My app's menu extra icon should reflect meaningful information to the user. However, there are too many cases to draw icons beforehand. It would be easier to programatically prepare the icon on the fly. What would be the best way to get the size of the NSImage to draw (in points or in pixels) and have a perfect look on retina and non-retina displays?

War es hilfreich?

Lösung

In AppKit, the size of the view in points is retrievable via the frame. Since 10.7, each screen additionally has a backingScaleFactor. The size in pixels that the view will therefore appear at on a given screen can be found by multiplying the size of the frame by the backing scale factor of the screen.

E.g. on a computer with exactly one screen you can do:

CGFloat backingScaleFactor = [[NSScreen mainScreen] backingScaleFactor];

NSSize pixelSize =
    NSSizeMake(
         view.frame.size.width * backingScaleFactor,
         view.frame.size.height * backingScaleFactor);

So I guess one answer is to take the largest scale factor of anything in [NSScreen screens].

EDIT: ... but a much better answer would be to use the view's convertSizeToBacking: — e.g.

pixelSize = [view convertSizeToBacking:view.frame.size];
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top