【问题标题】:Performance Improvements of CIFilter Chain?CIFilter 链的性能改进?
【发布时间】:2021-06-23 14:21:52
【问题描述】:

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

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
}

处理大约需要一秒钟。

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

【问题讨论】:

    标签: ios swift performance cifilter


    【解决方案1】:

    您应该使用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 很重要,因为创建它的成本很高,因为它保存并缓存了渲染图像所需的所有(金属)资源。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-25
      • 2015-03-21
      • 2016-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多