保存整个NSView内容,它位于NSScrollView中

Alt*_*cho 13 cocoa objective-c

我正在尝试将NSView的内容保存为图像,但是,只保存了在scrollview中可见的图像.

以下是视图中加载的实际图像: 实际形象.

但这就是我能够保存图像的方式: 保存的图像

有没有办法在NSView中保存整个图像?

这就是我如何保存我的NSView子类:

- (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];
}
Run Code Online (Sandbox Code Playgroud)

此方法位于我要保存的视图中.

ham*_*ene 18

使用 -[NSView cacheDisplayInRect:toBitmapImageRep:]

NSBitmapImageRep* rep = [self bitmapImageRepForCachingDisplayInRect:self.bounds];
[self cacheDisplayInRect:self.bounds toBitmapImageRep:rep];
Run Code Online (Sandbox Code Playgroud)