Home > Software design >  How to efficiently cascade CIFilters
How to efficiently cascade CIFilters

Time:11-19

Currently I'm cascading CIFilters like this:

    let filterA = CIFilter(...)
    let filterB = CIFilter(...)
    let filterC = CIFilter(...)

    var outputImage = CIImage(data: myInputData, options: [.applyOrientationProperty : true])

    filterA.setValue(outputImage, forKey: kCIInputImageKey)
    var outputImage = filterA.outputImage

    filterB.setValue(outputImage, forKey: kCIInputImageKey)
    var outputImage = filterB.outputImage

    filterC.setValue(outputImage, forKey: kCIInputImageKey)
    var outputImage = filterC.outputImage

    return render(ouputImage)

Is using one var outputImage in some way inefficient? Or in general words, what is the most performance efficient way of cascading CIFilters?

CodePudding user response:

Basically that’s fine. Whether you use those extra variables or not really doesn’t matter as Core Image only computes the effects once the image is rendered.

Personally I wouldn’t recommend reusing the same variable for each image, might get confusing as to which image is used where. Instead I’d directly pass the property from each filter:

filterA.setvalue(inputImage, forKey: kCIInputImageKey)
filterB.setValue(filterA.outputImage, forKey: kCIInputImageKey)
filterC.setValue(filterB.outputImage, forKey: kCIInputImageKey)

return render(filterC.outputImage)

The performance will be exactly the same.

  • Related