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.
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 parameterwindowIDshould bekCGNullWindowID.
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.
加载中,请稍侯......
精彩评论