Skip to content

Instantly share code, notes, and snippets.

@cbess
Last active November 22, 2018 15:51
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save cbess/a3bc4df1d101ca567a0ca8c9ace25553 to your computer and use it in GitHub Desktop.
NSWindow screenshot, best method found (objc)
// ref: https://stackoverflow.com/a/22967912/344591
@implementation NSView (Screenshot)
/// Returns an image that contains the entire window contents, including window background color and traffic lights.
/// Resembles the product of taking a screenshot using Preview.app
- (NSImage *)screenshotOfWindow {
CGWindowID winID = (CGWindowID) self.window.windowNumber;
CGImageRef ref = CGWindowListCreateImage(CGRectNull, kCGWindowListOptionIncludingWindow, winID, kCGWindowImageBoundsIgnoreFraming);
return [[NSImage alloc] initWithCGImage:ref size:self.bounds.size];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment