【问题标题】:How to combine filters如何组合过滤器
【发布时间】:2017-10-09 09:59:33
【问题描述】:

如何组合图像过滤器?比如我想用CIColorControls、CINoiseReduction等,是不是需要创建几个filter并初始化呢?还是有别的办法?

var colorControls: CIFilter
var noiseReduction: CIFilter
...

func init() {
   colorControls = CIFilter.init(name: "CIColorControls")!
   noiseReduction = CIFilter.init(name: "CINoiseReduction")!
   ...
}

【问题讨论】:

    标签: ios swift xcode core-graphics cifilter


    【解决方案1】:

    您可以一一应用过滤器:

    func filter(image: CIImage) -> CIImage? {
        guard let colorControls = CIFilter(name: "CIColorControls"),
              let noiseReduction = CIFilter(name: "CINoiseReduction") else { return nil }
        colorControls.setValue(image, forKey: kCIInputImageKey)
        ... // add some CIColorControls filter manipulations
        guard let imageWithFirstFilter = colorControls.value(forKey: kCIOutputImageKey) as? CIImage else { return nil }
        noiseReduction.setValue(imageWithFirstFilter, forKey: kCIInputImageKey)
        ... // add some CINoiseReduction filter manipulations
        guard let imageWithBothFilters = noiseReduction.value(forKey: kCIOutputImageKey) as? CIImage else { return nil }
        return imageWithBothFilters
    }
    

    有点不同的方式:

    func apply(_ filter: CIFilter?, for image: CIImage) -> CIImage {
        guard let filter = filter else { return image }
        filter.setValue(image, forKey: kCIInputImageKey)
        guard let filteredImage = filter.value(forKey: kCIOutputImageKey) else { return image }
        return filteredImage
    }
    

    使用:

    image = apply(colorControls, for: image)
    

    【讨论】:

    • 改了答案,可以用apply(_ filter, for image)func
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多