Search code examples
iosswiftperformancecore-imageciimage

Fastest way to render CIImage to JPEG?


I have a CIFilter pipeline.

It currently outputs UIImage using:

private func renderImage(ciImage: CIImage) -> UIImage?
{
    let cgImage = Filter.context.createCGImage(ciImage, from: ciImage.extent)

    return cgImage.map(UIImage.init)
}

This UIImage is later converted to JPEG and stored.

There's now a demand to filter images in bulk and skip the UIImage format. This needs to be done as fast as possible.

Here are a few ways to do this.

Which way is the fastest?


Solution

  • You can do that with the CIContext as well:

    let colorSpace = CGColorSpace(name: CGColorSpace.sRGB)!
    // if you need the JPEG data
    let data = Filter.context.jpegRepresentation(of: ciImage, colorSpace: colorSpace)
    // or if you want to write to file directly
    try Filter.context.writeJPEGRepresentation(of: ciImage, to: url, colorSpace: colorSpace)