Skip to content

Instantly share code, notes, and snippets.

@cobysy
Created June 24, 2014 16:51
Show Gist options
  • Save cobysy/eda7bfa7a3a9692f1636 to your computer and use it in GitHub Desktop.
Save cobysy/eda7bfa7a3a9692f1636 to your computer and use it in GitHub Desktop.
Draw NSImage (off-screen)
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