Saving CGImageRef to a png file?

前端 未结 3 1051
天涯浪人
天涯浪人 2020-11-30 00:28

in my Cocoa application, I load a .jpg file from disk, manipulate it. Now it needs to be written to disk as a .png file. How can you do that?

Thanks for your help!

相关标签:
3条回答
  • 2020-11-30 01:16

    Using CGImageDestination and passing kUTTypePNG is the correct approach. Here's a quick snippet:

    @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;
    }
    

    You'll need to add ImageIO and CoreServices (or MobileCoreServices on iOS) to your project and include the headers.


    If you're on iOS and don't need a solution that works on Mac too, you can use a simpler approach:

    // `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];
    

    In my tests, the ImageIO approach was about 10% faster than the UIImage approach 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.

    0 讨论(0)
  • 2020-11-30 01:21

    Create a CGImageDestination, passing kUTTypePNG as the type of file to create. Add the image, then finalize the destination.

    0 讨论(0)
  • 2020-11-30 01:29

    Here is a macOS-friendly, Swift 3 & 4 example:

    @discardableResult func writeCGImage(_ image: CGImage, to destinationURL: URL) -> Bool {
        guard let destination = CGImageDestinationCreateWithURL(destinationURL as CFURL, kUTTypePNG, 1, nil) else { return false }
        CGImageDestinationAddImage(destination, image, nil)
        return CGImageDestinationFinalize(destination)
    }
    
    0 讨论(0)
提交回复
热议问题