-
-
Save Noitidart/8affcd8bee60d22dcb52 to your computer and use it in GitHub Desktop.
objc/cg
This file contains 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
CGDirectDisplayID displays[32]; | |
uint32_t count; | |
//////////////////////////////////////////////////////////////// | |
if (CGGetActiveDisplayList(sizeof(displays)/sizeof(displays[0]), displays, &count) != kCGErrorSuccess) | |
{ | |
NSLog(@"failed to get display list"); | |
exit(EXIT_FAILURE); | |
} | |
//////////////////////////////////////////////////////////////// | |
CGRect rect = CGRectNull; | |
for (uint32_t i = 0; i < count; i++) { | |
// if display is secondary mirror of another display, skip it | |
if (CGDisplayMirrorsDisplay(displays[i]) != kCGNullDirectDisplay) | |
continue; | |
//////////////////////////////////////////////////////////////// | |
rect = CGRectUnion(rect, CGDisplayBounds(displays[i])); | |
//////////////////////////////////////////////////////////////// | |
} | |
//////////////////////////////////////////////////////////////// | |
NSBitmapImageRep* imageRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL | |
pixelsWide:CGRectGetWidth(rect) | |
pixelsHigh:CGRectGetHeight(rect) | |
bitsPerSample:8 | |
samplesPerPixel:4 | |
hasAlpha:YES | |
isPlanar:NO | |
colorSpaceName:NSCalibratedRGBColorSpace | |
bitmapFormat:0 | |
bytesPerRow:0 | |
bitsPerPixel:32]; | |
if (!imageRep) | |
{ | |
NSLog(@"failed to create bitmap image rep"); | |
exit(EXIT_FAILURE); | |
} | |
//////////////////////////////////////////////////////////////// | |
NSGraphicsContext* context = [NSGraphicsContext graphicsContextWithBitmapImageRep:imageRep]; | |
if (!context) | |
{ | |
NSLog(@"failed to create graphics context"); | |
exit(EXIT_FAILURE); | |
} | |
//////////////////////////////////////////////////////////////// | |
[NSGraphicsContext saveGraphicsState]; | |
//////////////////////////////////////////////////////////////// | |
{ | |
[NSGraphicsContext setCurrentContext:context]; | |
//////////////////////////////////////////////////////////////// | |
CGContextRef cgcontext = [context graphicsPort]; | |
//////////////////////////////////////////////////////////////// | |
CGContextClearRect(cgcontext, CGRectMake(0, 0, CGRectGetWidth(rect), CGRectGetHeight(rect))); | |
//////////////////////////////////////////////////////////////// | |
for (uint32_t i = 0; i < count; i++) | |
{ | |
// if display is secondary mirror of another display, skip it | |
if (CGDisplayMirrorsDisplay(displays[i]) != kCGNullDirectDisplay) | |
continue; | |
//////////////////////////////////////////////////////////////// | |
CGRect displayRect = CGDisplayBounds(displays[i]); | |
//////////////////////////////////////////////////////////////// | |
CGImageRef image = CGDisplayCreateImage(displays[i]); | |
if (!image) | |
continue; | |
//////////////////////////////////////////////////////////////// | |
CGRect dest = CGRectMake(displayRect.origin.x - rect.origin.x, | |
displayRect.origin.y - rect.origin.y, | |
displayRect.size.width, | |
displayRect.size.height); | |
//////////////////////////////////////////////////////////////// | |
CGContextDrawImage(cgcontext, dest, image); | |
//////////////////////////////////////////////////////////////// | |
CGImageRelease(image); | |
//////////////////////////////////////////////////////////////// | |
} | |
//////////////////////////////////////////////////////////////// | |
[[NSGraphicsContext currentContext] flushGraphics]; | |
} | |
//////////////////////////////////////////////////////////////// | |
[NSGraphicsContext restoreGraphicsState]; | |
//////////////////////////////////////////////////////////////// | |
NSData* data = [imageRep representationUsingType:NSPNGFileType properties:@{ }]; | |
//////////////////////////////////////////////////////////////// | |
[data writeToFile:@"/Users/noit/Desktop/full_ss.png" atomically:YES]; | |
//////////////////////////////////////////////////////////////// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment