Design Element
Comment on "Saving Cocoa View Contents to an Image File"
by Derek Kuhl — Oct 23
NSBitmapImageRep* rep = [self bitmapImageRepForCachingDisplayInRect:bounds];
[self cacheDisplayInRect:bounds toBitmapImageRep:rep];
NSData* data = [rep representationUsingType:NSJPEGFileType properties:nil];
[data writeToFile:@"/path/to/file.jpg" atomically:NO];

Works in a few less lines, doesn't need to declare an NSRect, doesn't worry about locking focus, and there's no worries about releasing any objects (rep is autoreleased).
Back to "Saving Cocoa View Contents to an Image File"
Design Element

Copyright © Scott Stevenson 2004-2015