Skip to content

Instantly share code, notes, and snippets.

@hkondo
Created June 23, 2014 22:23
Show Gist options
  • Select an option

  • Save hkondo/3498cc6f3b630ebe2f2a to your computer and use it in GitHub Desktop.

Select an option

Save hkondo/3498cc6f3b630ebe2f2a to your computer and use it in GitHub Desktop.
Capturing displays into PNG on OS X
void capture(int dispId) {
NSLog(@"Start capturing.");
CGImageRef imageRef = CGDisplayCreateImage(dispId);
NSLog(@"End");
NSURL *fileURL = [NSURL fileURLWithPath:[@"~/Desktop/screenshot.png" stringByExpandingTildeInPath]];
CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileURL, kUTTypePNG , 1, NULL);
CGImageDestinationAddImage(destination, imageRef, NULL);
CGImageDestinationFinalize(destination);
CFRelease(destination);
CFRelease(imageRef);
}
int main(int argc, const char * argv[])
{
NSArray *screenArray = [NSScreen screens];
for( NSScreen *screen in screenArray ){
NSDictionary *screenDescription = [screen deviceDescription];
NSNumber *displayID = [screenDescription objectForKey:@"NSScreenNumber"];
int dispId = [displayID intValue];
printf("%d\n", dispId);
capture(dispId);
}
// CGDirectDisplayID displayID = CGMainDisplayID();
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment