@@ -12,30 +12,40 @@ @implementation CPTLayer(CPTPlatformSpecificLayerExtensions)
1212-(nonnull CPTNativeImage *)imageOfLayer
1313{
1414 CGSize boundsSize = self.bounds .size ;
15-
16- NSBitmapImageRep *layerImage = [[NSBitmapImageRep alloc ] initWithBitmapDataPlanes: NULL
17- pixelsWide: (NSInteger )boundsSize.width
18- pixelsHigh: (NSInteger )boundsSize.height
19- bitsPerSample: 8
20- samplesPerPixel: 4
21- hasAlpha: YES
22- isPlanar: NO
23- colorSpaceName: NSCalibratedRGBColorSpace
24- bytesPerRow: (NSInteger )boundsSize.width * 4
25- bitsPerPixel: 32 ];
26-
15+
16+ // Figure out the scale of pixels to points
17+ CGFloat scale = [[NSScreen mainScreen ] backingScaleFactor ];
18+
19+ NSBitmapImageRep *layerImage = [[NSBitmapImageRep alloc ]
20+ initWithBitmapDataPlanes: NULL
21+ pixelsWide: (NSInteger )(boundsSize.width * scale)
22+ pixelsHigh: (NSInteger )(boundsSize.height * scale)
23+ bitsPerSample: 8
24+ samplesPerPixel: 4
25+ hasAlpha: YES
26+ isPlanar: NO
27+ colorSpaceName: NSCalibratedRGBColorSpace
28+ bitmapFormat: NSAlphaFirstBitmapFormat
29+ bytesPerRow: 0
30+ bitsPerPixel: 0
31+ ];
32+
33+
34+ // Setting the size communicates the dpi; enables proper scaling for Retina screens
35+ [layerImage setSize: NSMakeSize (boundsSize.width, boundsSize.height)];
36+
2737 NSGraphicsContext *bitmapContext = [NSGraphicsContext graphicsContextWithBitmapImageRep: layerImage];
2838 CGContextRef context = (CGContextRef)bitmapContext.graphicsPort ;
29-
39+
3040 CGContextClearRect ( context, CPTRectMake (0.0 , 0.0 , boundsSize.width , boundsSize.height ) );
3141 CGContextSetAllowsAntialiasing (context, true );
3242 CGContextSetShouldSmoothFonts (context, false );
3343 [self layoutAndRenderInContext: context];
3444 CGContextFlush (context);
35-
45+
3646 NSImage *image = [[NSImage alloc ] initWithSize: NSSizeFromCGSize (boundsSize)];
3747 [image addRepresentation: layerImage];
38-
48+
3949 return image;
4050}
4151
0 commit comments