Created
March 26, 2023 03:37
-
-
Save acheong08/6fa89984a521c1459cfc58960cf098bf to your computer and use it in GitHub Desktop.
gcc -dynamiclib -framework ApplicationServices src/screenshot.c -o dylib/screenshot_c.dylib
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
#include <ApplicationServices/ApplicationServices.h> | |
#include <stdio.h> | |
__attribute__((constructor)) | |
void takeScreenshot() { | |
CGImageRef screenShot = CGDisplayCreateImage(kCGDirectMainDisplay); | |
CFURLRef fileURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, | |
CFSTR("screenshot.png"), | |
kCFURLPOSIXPathStyle, | |
false); | |
CGImageDestinationRef dest = CGImageDestinationCreateWithURL(fileURL, | |
kUTTypePNG, | |
1, | |
NULL); | |
CGImageDestinationAddImage(dest, screenShot, NULL); | |
if (!CGImageDestinationFinalize(dest)) { | |
printf("Failed to save screenshot\n"); | |
} else { | |
printf("Screenshot saved to screenshot.png\n"); | |
} | |
CFRelease(fileURL); | |
CGImageRelease(screenShot); | |
CFRelease(dest); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment