ios - 如何组合过滤器

标签 ios swift xcode core-graphics cifilter

如何为图像组合滤镜?比如我想使用CIColorControls、CINoiseReduction等,是否需要创建多个过滤器并进行初始化?还是有别的办法?

var colorControls: CIFilter
var noiseReduction: CIFilter
...

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

最佳答案

您可以一个一个地应用过滤器:

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)

关于ios - 如何组合过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43897797/

相关文章:

android - Android Studio 有类似xcode 的autolayout 的功能吗?

iphone - 帮助格式化数字,objective-c

objective-c - 如何检查 NSURL 是否包含 NSString null?

ios - 如何在 iOS 4.2 中打印图像?

ios - 动画期间标签栏闪烁

ios - 使用 Floaty 库显示矩形按钮

ios - NavigationView 和 NavigationLink 在转换后不删除 View

ios - TableView 图像仅在向下滚动时显示

ios - XIB 中 UIImage 的色调

iOS StatusBar 颜色变化动画