将 NSBox 子类捕获为 PNG

发布于 2024-12-13 15:58:57 字数 620 浏览 3 评论 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 及其内容背后的任何内容?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天荒地未老 2024-12-20 15:58:57

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

- (void) drawRect:(NSRect)dirtyRect {

    NSDrawWindowBackground(dirtyRect);
    [super drawRect:dirtyRect];
}

Though not super-efficient, you might try drawing the window's background manually in the NSBox subclass:

- (void) drawRect:(NSRect)dirtyRect {

    NSDrawWindowBackground(dirtyRect);
    [super drawRect:dirtyRect];
}
标点 2024-12-20 15:58:57

你能从 Windows 角度捕获 NSBox 的矩形吗?

Could you capture the rectangle of the NSBox but from the windows perspective ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文