NSCursor: Using high-resolution cursors with cursor zoom (or retina)

不问归期 提交于 2019-12-04 10:07:58

问题


In OSX the user can zoom the mouse cursor using the accessibility system preferences. Since Lion (I think) OSX stores the cursors as PDFs and is able to resize them smoothly. I want the same functionality for my app but using PDFs as the NSImage used for my NSCursor just scales up the rendered bitmap when a cursor zoom level larger than 1.0 is set.

How do I:

  • Use vector artwork for my cursors and have them scale correctly like the system cursors do?
  • Detect the current cursor zoom level.
  • Get notified when the cursor zoom level changes?

Also, when using a HiDPI mode for my screen and revert the cursor zoom setting, the PDF cursor is blurred as well so how exactly do you guys retina-ify your cursors?


回答1:


I just got the solution told to me by @kongtomorrow. Here's the snippet he sent me:

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];

So essentially what this does is generate several image representations at the appropriate scale factors in the image, based on the original PDF. This works for me, my cursor is nice and smooth.



来源:https://stackoverflow.com/questions/19245387/nscursor-using-high-resolution-cursors-with-cursor-zoom-or-retina

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!