开发者

How to grab an image of an offscreen NSWindow?

开发者 https://www.devze.com 2023-01-22 21:51 出处:网络
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\'

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:

How to grab an image of an offscreen NSWindow?

and here's what I get (ignore the slightly larger size - that's my fault in the rendering of the image):

How to grab an image of an offscreen NSWindow?

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.


From the documentation comment for CGWindowListCreateImage in CGWindow.h:

kCGWindowListOptionAll, kCGWindowListOptionOnScreenOnly: Use all on-screen windows in this user session to construct the image. The parameter windowID should be kCGNullWindowID.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消