Created
June 24, 2014 16:51
-
-
Save cobysy/eda7bfa7a3a9692f1636 to your computer and use it in GitHub Desktop.
Draw NSImage (off-screen)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSBitmapImageRep* offscreenRep = [[NSBitmapImageRep alloc] | |
initWithBitmapDataPlanes:NULL | |
pixelsWide:size.width | |
pixelsHigh:size.height | |
bitsPerSample:8 | |
samplesPerPixel:4 | |
hasAlpha:YES | |
isPlanar:NO | |
colorSpaceName:NSCalibratedRGBColorSpace | |
bytesPerRow:0 | |
bitsPerPixel:0]; | |
NSGraphicsContext* nsContext = [NSGraphicsContext graphicsContextWithBitmapImageRep:offscreenRep]; | |
[NSGraphicsContext saveGraphicsState]; | |
[NSGraphicsContext setCurrentContext:nsContext]; | |
// Do the drawing using NSGraphics | |
// Do the drawing using Core Graphics | |
CGContextRef cgContext = [nsContext graphicsPort]; | |
NSGraphicsContext restoreGraphicsState]; | |
NSImage* image = [[NSImage alloc] initWithSize:size]; | |
[image addRepresentation:offscreenRep]; | |
return image; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment