Convert CIImage to Data without loading image into memory Swift

Viewed 52

I'm trying to convert a CIImage to a Data object like so:

let context = CIContext(options: [.useSoftwareRenderer: false])

if let colorSpace = CGColorSpace(name: CGColorSpace.sRGB) {
    let croppedImageData = context.jpegRepresentation(of: ciImage, colorSpace: colorSpace)
}

This works and the result is fine however when looking at the allocations in instruments every time I run this piece of code the memory allocated is upwards of 240 MB which is obviously way to much.

I would like to simply convert the CIImage to Data (for storing reasons) without it taking up so much memory even if it's just a split second.

I've tried to use autoreleasepool and tried it on another thread but the jumps in allocations remains the same.

Image allocation

Thankfully it doesn't stay in memory and is released but the fact that it spikes is troublesome. The CIImage extent is (0.0, 0.0, 2250.0, 3000.0) which probably why it's taking so much memory to convert.

Suggestions are welcome :)

0 Answers
Related