CIFilter链的性能提升?

Performance Improvements of CIFilter Chain?

在照片应用程序(无视频)中,我有许多内置和自定义的 Metal CIFilters 在 class 中链接在一起,就像这样(我省略了设置过滤器参数的行,除了输入图像):

var colorControlsFilter = CIFilter(name: "CIColorControls")!
var highlightShadowFilter = CIFilter(name: "CIHighlightShadowAdjust")!

func filter(image data: Data) -> UIImage
{
    var outputImage: CIImage?

    let rawFilter = CIFilter(imageData: imageData, options: nil)
    outputImage = rawFilter?.outputImage

    colorControlsFilter.setValue(outputImage, forKey: kCIInputImageKey)
    outputImage = colorControlsFilter.setValue.outputImage

    highlightShadowFilter.setValue(outputImage, forKey: kCIInputImageKey)
    outputImage = highlightShadowFilter.setValue.outputImage

    ...
    ...

    if let ciImage = outputImage
    {
        return renderImage(ciImage: ciImage)
    }
}

func renderImage(ciImage: CIImage) -> UIImage?
{
    var outputImage: UIImage?
    let size = ciImage.extent.size

    UIGraphicsBeginImageContext(size)
    if let context = UIGraphicsGetCurrentContext()
    {
        context.interpolationQuality = .high
        context.setShouldAntialias(true)

        let inputImage = UIImage(ciImage: ciImage)
        inputImage.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))

        outputImage = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()
    }

    return outputImage
}

处理大约需要一秒钟。

这种将过滤器的输出连接到输入的方式是否最有效?或者更笼统地说:我可以做哪些性能优化?

您应该使用 CIContext 渲染图像:

var context = CIContext() // create this once and re-use it for each image

func render(image ciImage: CIImage) -> UIImage? {
    let cgImage = context.createCGImage(ciImage, from: ciImage.extent)
    return cgImage.map(UIImage.init)
}

只创建一次 CIContext 很重要,因为创建它的成本很高,因为它持有并缓存渲染图像所需的所有(金属)资源。