0

我目前正在尝试将我的 NSBox 子类的内容捕获到 PNG 文件中。我发现了一些似乎可以完美解决问题的代码(我从我的实际子类中调用的代码):

[self lockFocus];

NSBitmapImageRep *rep = [self bitmapImageRepForCachingDisplayInRect:[self bounds]];

[self cacheDisplayInRect:[self bounds] toBitmapImageRep:rep];

[self unlockFocus];

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

[imageData writeToFile:@"~/Desktop/test.png" atomically:NO];    

正如我所说,该代码运行良好,但只有一个小问题:NSBox 实际上是透明的,因此对于习惯了默认的灰白色背景的用户来说,图像看起来很奇怪。

我还没有从我的主要 AppDelegate 中成功调用它。那应该可以解决问题,也可以捕获 NSBox 背后的内容,但这对我不起作用。

有谁知道如何捕捉这个 NSBox 背后的任何东西及其内容?

4

2 回答 2

0

虽然效率不高,但您可以尝试在 NSBox 子类中手动绘制窗口背景:

- (void) drawRect:(NSRect)dirtyRect {

    NSDrawWindowBackground(dirtyRect);
    [super drawRect:dirtyRect];
}
于 2011-11-04T22:27:24.227 回答
0

你能从 windows 的角度捕捉 NSBox 的矩形吗?

于 2011-11-04T22:31:08.650 回答