因为,我需要一些高级菜单栏项,所以我使用自定义 NSView 子类作为 menubaritem 的视图。
在 drawrect 中,我正在绘制一些简单的 nsimage,它适用于普通显示器,但在视网膜显示器上,它加载较小的图像(是的,我有 @2x 艺术品)。
代码:
- (void)drawRect:(NSRect)dirtyRect
{
[self.statusItem drawStatusBarBackgroundInRect:dirtyRect withHighlight:self.isHighlighted];
[icon scalesWhenResized];
icon = self.isHighlighted ? self.alternateImage : [images objectAtIndex:index];
NSSize iconSize = [icon size];
NSRect bounds = self.bounds;
CGFloat iconX = roundf((NSWidth(bounds) - iconSize.width) / 2);
CGFloat iconY = roundf((NSHeight(bounds) - iconSize.height) / 2);
NSPoint iconPoint = NSMakePoint(iconX, iconY);
[icon drawAtPoint:iconPoint fromRect:bounds operation:NSCompositeSourceOver fraction:1];
}
我用 Quartz Debug 和真正的 Retina Macbook 对其进行了测试,除了菜单栏中的这个小图像外,一切都很好。
更新:
self.images = [NSArray arrayWithObjects:[NSImage imageNamed:@"1.png"],[NSImage imageNamed:@"2.png"],[NSImage imageNamed:@"3.png"],[NSImage imageNamed:@"4.png"],[NSImage imageNamed:@"5.png"],[NSImage imageNamed:@"6.png"],[NSImage imageNamed:@"7.png"],[NSImage imageNamed:@"8.png"],[NSImage imageNamed:@"9.png"],[NSImage imageNamed:@"10.png"],[NSImage imageNamed:@"11.png"],[NSImage imageNamed:@"12.png"],[NSImage imageNamed:@"13.png"],[NSImage imageNamed:@"14.png"],[NSImage imageNamed:@"15.png"],[NSImage imageNamed:@"16.png"],[NSImage imageNamed:@"17.png"],[NSImage imageNamed:@"18.png"],[NSImage imageNamed:@"19.png"],[NSImage imageNamed:@"20.png"],[NSImage imageNamed:@"21.png"],[NSImage imageNamed:@"22.png"],[NSImage imageNamed:@"23.png"],[NSImage imageNamed:@"24.png"],[NSImage imageNamed:@"25.png"],[NSImage imageNamed:@"26.png"],
nil];