Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save hkondo/a6cd3cc11bd628c139a3 to your computer and use it in GitHub Desktop.
Capture Screen Images
//
// main.m
// Screenshot
//
// Created by Hideki KONDO on 2014/06/14.
// Copyright (c) 2014年 Hideki KONDO. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
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