• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Saving PNG files from NSImage
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

References: 
 >Saving PNG files from NSImage (From: Martin Linklater <email@hidden>)

  • Prev by Date: Re: Saving PNG files from NSImage
  • Next by Date: ARC + CF types
  • Previous by thread: Saving PNG files from NSImage
  • Next by thread: Re: Saving PNG files from NSImage
  • Index(es):
    • Date
    • Thread