将 NSBox 子类捕获为 PNG
我目前正在尝试将 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 及其内容背后的任何内容?
I'm currently trying to capture the contents of my NSBox subclass to a PNG file. I've found some code that seems to do the trick perfectly (code that I call from my actual subclass):
[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];
As I said, that code works quite well, but there's only one small issue: the NSBox is actually transparent, so the image looks very strange to the user, who's used to having the default whitish-gray background.
I haven't succesfully called this from my main, AppDelegate. That should solve the problem, capturing what's behind the NSBox as well, but it's not working for me.
Does anyone know how to capture anything behind this NSBox as well as its contents?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然不是超级高效,但您可以尝试在 NSBox 子类中手动绘制窗口的背景:
Though not super-efficient, you might try drawing the window's background manually in the NSBox subclass:
你能从 Windows 角度捕获 NSBox 的矩形吗?
Could you capture the rectangle of the NSBox but from the windows perspective ?