NSView drawRect和视网膜图像

时间:2013-02-14 20:27:09

标签: objective-c macos cocoa core-graphics

因为,我需要一些高级菜单栏项,我使用自定义NSView子类作为menubaritem的视图。

在绘图中,我正在绘制一些简单的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];

1 个答案:

答案 0 :(得分:3)

这可能是在黑暗中拍摄的,但如果您正在使用扩展程序加载图像,请尝试删除它。

而不是: [NSImage imageNamed:@"icon.png"]执行[NSImage imageNamed:@"icon"]

Xcode将“将高分辨率图稿组合”设置为YES,使您的图像合并为TIFF文件。因此,捆绑包中不存在PNG文件,因此不会加载。