NSCursor:使用带有光标变焦(或视网膜)的高分辨率光标

时间:2013-10-08 10:35:53

标签: macos cocoa cursor nscursor

在OSX中,用户可以使用辅助功能系统首选项缩放鼠标光标。由于Lion(我认为)OSX将游标存储为PDF并且能够平滑地调整它们的大小。我希望我的应用程序具有相同的功能,但使用PDF作为我NSImage使用的NSCursor只是在设置了大于1.0的光标缩放级别时缩放渲染的位图。

我如何:

  • 为我的游标使用矢量图稿并让它们像系统游标那样正确缩放吗?
  • 检测当前光标缩放级别。
  • 当光标缩放级别改变时收到通知?

此外,当我的屏幕使用HiDPI模式并恢复光标缩放设置时,PDF光标也会模糊,那么你们究竟是如何调整你的光标?

1 个答案:

答案 0 :(得分:5)

我刚刚通过@kongtomorrow告诉了我解决方案。这是他发给我的片段:

NSImage *   theImage = [NSImage imageNamed: @"CURS_128.pdf"];

NSImage *resultImage = [[NSImage alloc] initWithSize:[theImage size]];

for (int scale = 1; scale <= 4; scale++) {
    NSAffineTransform *xform = [[NSAffineTransform alloc] init];
    [xform scaleBy:scale];
    id hints = @{ NSImageHintCTM: xform };
    CGImageRef rasterCGImage = [theImage CGImageForProposedRect:NULL context:nil hints:hints];
    NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithCGImage:rasterCGImage];
    [rep setSize:[theImage size]];
    [resultImage addRepresentation:rep];
}

NSCursor*   theCursor = [[NSCursor alloc] initWithImage: resultImage hotSpot: NSMakePoint(12,8)];
[self.scrollView setDocumentCursor: theCursor];

基本上,这样做可以根据原始PDF在图像中的适当比例因子处生成多个图像表示。这对我有用,我的光标很漂亮,光滑。