Skip to content

Instantly share code, notes, and snippets.

@ashfurrow
Created February 28, 2012 22:37
Show Gist options
  • Save ashfurrow/1935760 to your computer and use it in GitHub Desktop.
Save ashfurrow/1935760 to your computer and use it in GitHub Desktop.
Write both files to disk
NSRect offscreenRect = NSMakeRect(0.0, 0.0, 20.0, 12.0);
NSBitmapImageRep* bitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:nil
pixelsWide:offscreenRect.size.width
pixelsHigh:offscreenRect.size.height
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSCalibratedRGBColorSpace
bitmapFormat:0
bytesPerRow:(4 * offscreenRect.size.width)
bitsPerPixel:32];
NSGraphicsContext* originalContext = [NSGraphicsContext currentContext];
[NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithBitmapImageRep:bitmap]];
[NSGraphicsContext saveGraphicsState];
//clear the image rep. This is faster than filling with [NSColor clearColor].
unsigned char *bitmapData = [bitmap bitmapData];
if (bitmapData)
bzero(bitmapData, [bitmap bytesPerRow] * [bitmap pixelsHigh]);
NSRect shadowRect = centeredRect;
shadowRect.origin.y -= 1.0f;
[shadowImage drawInRect:NSMakeRect(0, 0, 20, 11) fromRect:NSRectFromCGRect([shadowImage extent]) operation:NSCompositeSourceOver fraction:0.75f];
[drawnImage drawInRect:NSMakeRect(0, 1, 20, 11) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
//restore the original graphics context
[NSGraphicsContext restoreGraphicsState];
[NSGraphicsContext setCurrentContext:originalContext];
//get PNG data from the image rep
NSData* pngData = [bitmap representationUsingType:NSPNGFileType properties:nil];
NSError* error;
if(![pngData writeToURL:[NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%ld.png", frameNumber+1]]] options:NSDataWritingAtomic error:&error])
{
NSLog(@"%@",error);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment