Created
June 23, 2014 22:23
-
-
Save hkondo/3498cc6f3b630ebe2f2a to your computer and use it in GitHub Desktop.
Capturing displays into PNG on OS X
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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