Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 682
danwood Avatar asked Dec 10 '22 19:12

danwood


1 Answers

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

like image 89
Mike Abdullah Avatar answered May 21 '23 17:05

Mike Abdullah