views:

129

answers:

1

I have a very simple bit of code that is supposed to capture the bitmap of a view. This used to work in Leopard, but seems horribly broken in Snow Leopard.

Here is the code, responding to a button press on the window:

- (IBAction)snapshot:(id)sender
{
 NSView* view = [[sender window] contentView];
 NSBitmapImageRep* bitmap
  = [view bitmapImageRepForCachingDisplayInRect:[view bounds]];
 NSData *tiff = [bitmap TIFFRepresentation];
 [tiff writeToFile:[@"~/Desktop/snapshot.tiff" stringByExpandingTildeInPath]
     atomically:YES];
}

Clicking on the button to take a snapshot just results in a fully transparent image.

Am I just completely clueless here, or is this bitmap caching method broken?

A simple project — basically a starter NSDocument project, with a button that calls this code -- can be found here.

+2  A: 

-bitmapImageRepForCachingDisplayInRect: doesn't actually capture anything; it just generates a blank bitmap ready for caching. You need to call -cacheDisplayInRect:toBitmapImageRep: to do that.

Mike Abdullah