Saving Cocoa View Contents to an Image File

I can't ignore an impassioned plea. AnonymousFred posted an emotionally-charged comment to the entry announcing the Cocoa Graphics Tutorial. Fred just wants to know how to export his view contents as JPEG or BMP. Here's a small snippet.

Implement the code as follows in your controller:

- (IBAction)export:(id)sender
  [view lockFocus];
  NSRect bounds = [view bounds];
  NSBitmapImageRep* rep;
  rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:bounds];
  [view unlockFocus];
  NSData* data;
  data = [rep representationUsingType:NSJPEGFileType properties:nil];
  [data writeToFile:@"/path/to/file.jpg" atomically:NO];
  [rep release];

The "view" object should probably be an IBOutlet connected to the view whose contents you want to export. You can also use NSBMPFileType as the type to get a bmp file, though you obviously need to change the filename.

Rest well.

(Thanks to Jesper/Waffle for pointing me at the easier solution.)
Design Element
Saving Cocoa View Contents to an Image File
Posted Oct 22, 2007 — 5 comments below


Derek Kuhl — Oct 23, 07 4818

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).

Steve Weller — Oct 23, 07 4819

I have a name for this: it's the knowledgeable novices syndrome.

Ignorant novices feed fine by nibbling on the tasty morsels of code that Scott and other serve up. Ignorant experts need help with the unfamiliar utensils and spices, but are otherwise great code cooks. Knowledgeable experts make entire code meals table-side from raw reference materials in real time.

Knowledgeable novices are a challenge. They just want to make and eat a sandwich, but are having the darndest time doing it. Their knowledge is actually an encumberance, since they must unlearn what they think they know about sandwiches in order to make one the Cocoa way. There is no cutting of bread and spreading of peanut butter as they are sure there must be; just lamination, repitition, and bounding polygons applied to a couple of raw materials.

Jon Hess — Oct 23, 07 4820

The -[NSView cacheDisplayInRect: ... ] method will be much more pleasing if your view isn't opaque. cacheDisplayInRect: will only draw your view and won't capture any of the views behind it. The lock focus and use a bitmap image rep technique will end up picking up any of the views that 'show through' your view from behind.

For example, the cacheDisplayInRect method on a push button will only grab the push button while the first method will get the push button and the window background pattern.

Blain — Oct 24, 07 4825

With the logical, reasoned comment that ends in ljkgkhljk, and the mental image of a laminated sandwich, this article is full of awesome. That is all.

msykes — Mar 05, 08 5590

Perfect - this is exactly what I was looking for, thank you very much.


Comments Temporarily Disabled

I had to temporarily disable comments due to spam. I'll re-enable them soon.

Copyright © Scott Stevenson 2004-2015