Last active
April 19, 2024 07:55
-
-
Save theevilbit/d2580c5d12224f3291dfbb74fd6af29b to your computer and use it in GitHub Desktop.
Make a screenshot on macOS using Objective-C
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
/* | |
Compile: | |
gcc -framework Foundation -framework AppKit screenshot.m -o screenshot | |
*/ | |
#import <Foundation/Foundation.h> | |
#import <AppKit/AppKit.h> | |
int main(void) { | |
//capture window | |
CGImageRef screenshot = CGWindowListCreateImage(CGRectInfinite, kCGWindowListOptionOnScreenOnly, kCGNullWindowID, kCGWindowImageDefault); | |
//create bitmap | |
NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithCGImage:screenshot]; | |
//create PNG from bitmap | |
NSData *data = [bitmap representationUsingType:NSBitmapImageFileTypePNG properties:NULL]; | |
//save file | |
[data writeToFile: @"screenshot.png" atomically: NO]; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment