This code used to work, however I think Xcode\'s new ARC may have killed it
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
CGDir
The code you posted wouldn't work either with or without ARC, because you need to expand the tilde in the path name before passing it.
The code you posted also was leaking the items returned by CGDisplayCreateImage
and CGImageDestinationCreateWithURL
. Here's an example that works and doesn't leak:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
CGDirectDisplayID displayID = CGMainDisplayID();
CGImageRef imageRef = CGDisplayCreateImage(displayID); //this is a screenshot (works fine)
NSString *path = [@"~/Desktop/public.png" stringByExpandingTildeInPath];
[self savePNGImage:imageRef path:path];
CFRelease(imageRef);
}
- (void)savePNGImage:(CGImageRef)imageRef path:(NSString *)path
{
NSURL *fileURL = [NSURL fileURLWithPath:path];
CGImageDestinationRef dr = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileURL, kUTTypePNG , 1, NULL);
CGImageDestinationAddImage(dr, imageRef, NULL);
CGImageDestinationFinalize(dr);
CFRelease(dr);
}