Re: Saving PNG files from NSImage
Re: Saving PNG files from NSImage
- Subject: Re: Saving PNG files from NSImage
- From: Andy Lee <email@hidden>
- Date: Mon, 29 Aug 2011 10:05:49 -0400
I don't know if this would be the most efficient way, but if you can get your image as a CGImageRef you can use the functions below. They work for me. You might want to handle errors better.
There are tips at <http://www.cocoadev.com/index.pl?ConvertNSImageToCGImage> for getting a CGImageRef.
--Andy
==========
BOOL ALExportCGImageWithType(CGImageRef imageRef, NSString *imageFilePath, CFStringRef imageTypeRef)
{
NSURL *imageFileURL = [NSURL fileURLWithPath:imageFilePath];
if (imageFileURL == nil)
{
NSLog(@"[ERROR] %s -- Could not create an NSURL from '%@'.", __PRETTY_FUNCTION__, imageFilePath);
return NO;
}
CGImageDestinationRef imageDestRef = CGImageDestinationCreateWithURL((CFURLRef)imageFileURL,
imageTypeRef,
1,
NULL);
if (imageDestRef == NULL)
{
NSLog(@"[ERROR] %s -- Failed to create CGImageDestinationRef.", __PRETTY_FUNCTION__);
return NO;
}
CGImageDestinationAddImage(imageDestRef, imageRef, NULL);
if (!CGImageDestinationFinalize(imageDestRef))
{
NSLog(@"[ERROR] %s -- Failed to save image to '%@' with type '%@'.", __PRETTY_FUNCTION__, imageFilePath, imageTypeRef);
return NO;
}
return YES;
}
BOOL ALExportCGImageAsPNG(CGImageRef imageRef, NSString *imageFilePath)
{
return ALExportCGImageWithType(imageRef, imageFilePath, kUTTypePNG);
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden