capture hidden NSWindow

主宰稳场 提交于 2019-12-06 16:08:21

It isn't possible to capture the contents of an NSWindow which isn't visible on-screen. When a window is minimized/hidden/whatever, the visual representation is dropped to save memory.

(Not sure on exactly how this is managed, feel free to chime in if you have a deeper knowledge of the window system. I only know this from experience, trying to capture windows with CamTwist and BoinxTV.)

user2067656

You can do it by:

NSImage *img = [[NSImage alloc] initWithCGImage:[window windowImageShot] size:window.frame.size];

category to NSWindow:

- (CGImageRef)windowImageShot
{
    CGWindowID windowID = (CGWindowID)[self windowNumber];
    CGWindowImageOption imageOptions = kCGWindowImageBoundsIgnoreFraming | kCGWindowImageNominalResolution;
    CGWindowListOption singleWindowListOptions = kCGWindowListOptionIncludingWindow;
    CGRect imageBounds = CGRectNull;

    CGImageRef windowImage = CGWindowListCreateImage(imageBounds, singleWindowListOptions, windowID, imageOptions);

    return windowImage;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!