Why is bitmapImageRepForCachingDisplayInRect: creating an empty image?
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 T开发者_如何学GoIFFRepresentation];
[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.
-bitmapImageRepForCachingDisplayInRect:
doesn't actually capture anything; it just generates a blank bitmap ready for caching. You need to call -cacheDisplayInRect:toBitmapImageRep:
to do that.
精彩评论