Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving entire NSView contents, which is inside an NSScrollView

I'm trying to save contents of an NSView as an image, but, only the image visible inside the scrollview is saved.

Here is the actual image loaded in the view: actual image.

But this is how I'm able to save the image: saved image

Is there any way to save the entire image in the NSView?

This is how I'm saving my NSView subclass:

- (void)save
{
    [self lockFocus];
    NSBitmapImageRep* rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:[self bounds]];  
    [self unlockFocus];

    NSData* data = [rep representationUsingType:NSPNGFileType properties:nil];

    NSString* string = NSHomeDirectory();
    NSString* pth1 = [string stringByAppendingPathComponent:@"ttx.png"];

    [data writeToFile:pth1 atomically:YES];
}

This method is in the View i want to save.

like image 358
Alterecho Avatar asked Mar 02 '12 17:03

Alterecho


1 Answers

Use -[NSView cacheDisplayInRect:toBitmapImageRep:]

NSBitmapImageRep* rep = [self bitmapImageRepForCachingDisplayInRect:self.bounds];
[self cacheDisplayInRect:self.bounds toBitmapImageRep:rep];
like image 110
hamstergene Avatar answered Sep 19 '22 18:09

hamstergene