我目前正在尝试将我的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实际上是透明的,所以对于习惯了默认白灰色背景的用户来说,图像看起来非常奇怪。
我还没有成功地从我的main中调用它,AppDelegate。这应该可以解决问题,也可以捕获NSBox背后的内容,但它对我不起作用。
有谁知道如何捕获此NSBox背后的内容以及其内容吗?
发布于 2011-11-05 06:27:24
虽然效率不高,但您可以尝试在NSBox子类中手动绘制窗口的背景:
- (void) drawRect:(NSRect)dirtyRect {
NSDrawWindowBackground(dirtyRect);
[super drawRect:dirtyRect];
}发布于 2011-11-05 06:31:09
你能从窗口的角度捕捉到NSBox的矩形吗?
https://stackoverflow.com/questions/8015836
复制相似问题