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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to