Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using <code>CGImageDestination</code> and passing <code>kUTTypePNG</code> is the correct approach. Here's a quick snippet:</p> <pre><code>@import MobileCoreServices; // or `@import CoreServices;` on Mac @import ImageIO; BOOL CGImageWriteToFile(CGImageRef image, NSString *path) { CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:path]; CGImageDestinationRef destination = CGImageDestinationCreateWithURL(url, kUTTypePNG, 1, NULL); if (!destination) { NSLog(@"Failed to create CGImageDestination for %@", path); return NO; } CGImageDestinationAddImage(destination, image, nil); if (!CGImageDestinationFinalize(destination)) { NSLog(@"Failed to write image to %@", path); CFRelease(destination); return NO; } CFRelease(destination); return YES; } </code></pre> <p>You'll need to add <code>ImageIO</code> and <code>CoreServices</code> (or <code>MobileCoreServices</code> on iOS) to your project and include the headers.</p> <hr> <p>If you're on iOS and don't need a solution that works on Mac too, you can use a simpler approach:</p> <pre><code>// `image` is a CGImageRef // `path` is a NSString with the path to where you want to save it [UIImagePNGRepresentation([UIImage imageWithCGImage:image]) writeToFile:path atomically:YES]; </code></pre> <p><strong>In my tests, the ImageIO approach was about 10% faster than the UIImage approach</strong> on my iPhone 5s. In the simulator, the UIImage approach was faster. It's probably worth testing each for your particular situation on the device if you're really concerned with performance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload