18

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?

4

1 回答 1

6

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.

于 2015-01-30T23:15:35.883 回答