Search code examples
iosswiftciimageuiimagejpegrepresentationphotokit

Given a CIImage, what is the fastest way to write image data to disk?


I'm working with PhotoKit and have implemented filters users can apply to photos in their Photo Library. I am currently obtaining the image, applying a filter, returning the edited version as a CIImage, then I convert the CIImage into NSData using UIImageJPEGRepresentation so I may write that out to disk. While this works beautifully, when users attempt to edit really large (like 30 MB) photos it can take upwards of 30 seconds for this to occur, with 98% of the time spent on UIImageJPEGRepresentation (stat obtained from Instruments).

I am looking for a more efficient way to save this edited photo to disk without compromising quality, if possible.

I understand UIImagePNGRepresentation may result in improved quality, but this is even slower than the JPEG representation.

This is what I am currently doing, on the default priority thread (qos_class_utility):

func jpegRepresentationOfImage(image: CIImage) -> NSData {
    let eaglContext = EAGLContext(API: .OpenGLES2)
    let ciContext = CIContext(EAGLContext: eaglContext)

    let outputImageRef = ciContext.createCGImage(image, fromRect: image.extent())
    let uiImage = UIImage(CGImage: outputImageRef, scale: 1.0, orientation: UIImageOrientation.Up)

    return UIImageJPEGRepresentation(uiImage, 0.9) //this takes upwards of 20-30 seconds with large photos!
}

//writing out to disk:
var error: NSError?
let success = jpegData.writeToURL(contentEditingOutput.renderedContentURL, options: NSDataWritingOptions.AtomicWrite, error: &error)

Solution

  • I would suggest passing the CGImage directly to ImageIO using CGImageDestination. You can pass a dictionary to CGImageDestinationAddImage to indicate the compression quality, image orientation, etc.

    CFDataRef save_cgimage_to_jpeg (CGImageRef image)
    {
        CFMutableDataRef cfdata = CFDataCreateMutable(nil,0);
        CGImageDestinationRef dest = CGImageDestinationCreateWithData(data, CFSTR("public.jpeg"), 1, NULL);
        CGImageDestinationAddImage(dest, image, NULL);
        if(!CGImageDestinationFinalize(dest))
            ; // error
        CFRelease(dest);
        return cfdata
    }