如何抓取屏幕外 NSWindow 的图像?
我正在尝试获取屏幕外窗口的图像以在基于 CALayer 的动画中使用,但无论我尝试哪种方法,我都无法从另一侧获取图像。这是我在项目中的自定义 NSWindow
子类上使用的代码:
CGImageRef windowImage = CGWindowListCreateImage(CGRectNull,
kCGWindowListOptionAll,
(CGWindowID)[self windowNumber],
kCGWindowImageDefault);
图像应如下所示:
这是我得到的结果(忽略稍大的尺寸 - 这是我在图像渲染中的错误):
谁能看出我哪里出错了?奇怪的是,Quartz Debug 可以毫无问题地看到窗口图像,但是 Apple 的 Grab 示例代码之子 不能。
I'm trying to get an image of an offscreen window to use in a CALayer
-based animation, but no matter which method I try I cannot get an image out the other side. Here is the code I've been using on a custom NSWindow
subclass in my project:
CGImageRef windowImage = CGWindowListCreateImage(CGRectNull,
kCGWindowListOptionAll,
(CGWindowID)[self windowNumber],
kCGWindowImageDefault);
This is what the image should look like:
and here's what I get (ignore the slightly larger size - that's my fault in the rendering of the image):
Can anyone see where I'm going wrong? Strangely, Quartz Debug can see the window image without issue, but the Son of Grab sample code from Apple cannot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 CGWindow.h 中
CGWindowListCreateImage
的文档注释:这就是为什么您要获取您感兴趣的矩形内的所有窗口的屏幕截图。
要获取特定窗口的屏幕截图,请使用
kCGWindowListOptionInclusionWindow
。From the documentation comment for
CGWindowListCreateImage
in CGWindow.h:That's why you're getting a screenshot of all the windows that are within the rectangle you're interested in.
To take a screenshot of a specific window, use
kCGWindowListOptionIncludingWindow
.