在Swift中加快UIImage处理速度

问题描述 投票:0回答:1

我正在尝试应用下一个代码来对从图库中拍摄的图像进行着色:

func zoeFilter() -> UIImage? {
        let brownColor = UIColor (red: 128/255, green: 0, blue: 0, alpha: 1)
        let colorRect = CGRect (origin: .zero, size: self.size)

        let renderer = UIGraphicsImageRenderer (bounds: colorRect)

        let brownColoredImage = renderer.image { ctx in
            ctx.cgContext.setFillColor(greyColor.cgColor)
            ctx.fill(colorRect)
        }

        let outBrown0Image = renderer.image { ctx in
            ctx.cgContext.setFillColor(UIColor.white.cgColor)
            ctx.fill(colorRect)
            self.draw(in: colorRect, blendMode: .normal, alpha: 1)
            brownColoredImage.draw(in: colorRect, blendMode: .colorDodge, alpha: 1)
        }

        let outBrownImage = renderer.image { ctx in
            self.draw(in: colorRect, blendMode: .normal, alpha: 1)
            outBrown0Image.draw(in: colorRect, blendMode: .multiply, alpha: 1)
        }

        return outBrownImage
    }

但是处理图像大约需要14秒钟,这对用户体验来说不是很好...

您知道如何获得相同的结果但加速图像处理吗?

你有小费吗?

提前感谢!

最终解决方案>>] >>

func zoeFilter() -> UIImage? {
        let inImage = CIImage (image: self)

        let SRGBImage = inImage?.applyingFilter("CILinearToSRGBToneCurve")

        let brownColor = CIColor (red: 128/255, green: 0, blue: 0, alpha: 1)
        let colorRect = CGRect (origin: .zero, size: self.size)

        dynamic let colorFilter = CIFilter(name: "CIConstantColorGenerator")
        dynamic let colorizeFilter = CIFilter (name: "CIColorDodgeBlendMode")
        dynamic let multiplyFilter = CIFilter (name: "CIMultiplyBlendMode")

        colorFilter?.setValue(brownColor, forKey: kCIInputColorKey)
        let brownSolidImage = colorFilter?.outputImage?.cropped(to: colorRect)

        colorizeFilter?.setValue(SRGBImage, forKey: kCIInputBackgroundImageKey)
        colorizeFilter?.setValue(brownSolidImage, forKey: kCIInputImageKey)
        let outBrown0Image = colorizeFilter?.outputImage

        multiplyFilter?.setValue(SRGBImage, forKey: kCIInputBackgroundImageKey)
        multiplyFilter?.setValue(outBrown0Image, forKey: kCIInputImageKey)
        let outBrownImage = multiplyFilter?.outputImage

        let linearImage = outBrownImage?.applyingFilter("CISRGBToneCurveToLinear")

        let cgImage = CIContext().createCGImage(linearImage!, from: linearImage!.extent)
        return UIImage (cgImage: cgImage!)
    }

我正在尝试应用下一个代码来着色从图库中获取的图像:func zoeFilter()-> UIImage? {let brownColor = UIColor(红色:128/255,绿色:0,蓝色:0,alpha:1)...

swift xcode image-processing uiimage
1个回答
1
投票

最容易尝试的方法是将这3个renderer.image { ... }呼叫折叠为一个呼叫。您应该能够将第二个2个调用的内容移到第一个中。这将产生巨大的性能差异。

[除此之外,您真的需要对相机胶卷上的全尺寸图像执行此操作吗?如果不是,请在执行所有不需要的渲染之前调整在渲染器上设置的大小。

© www.soinside.com 2019 - 2024. All rights reserved.