Skip to content

Instantly share code, notes, and snippets.

@acheong08
Created March 26, 2023 03:37
Show Gist options
  • Save acheong08/6fa89984a521c1459cfc58960cf098bf to your computer and use it in GitHub Desktop.
Save acheong08/6fa89984a521c1459cfc58960cf098bf to your computer and use it in GitHub Desktop.
gcc -dynamiclib -framework ApplicationServices src/screenshot.c -o dylib/screenshot_c.dylib
#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