这是一个专注于为 Swift 带来抖动效果的库,因为用于抖动的 CIFilter 主要是噪声。 该实现是通过 Accelerate 框架完成的。
要使用该库,您将拥有一些基于 UIImages 和 CIFilter 的访问点。
CPU
import SwiftDithering
function yourFunction() {
//Highly Recommended
Task{
do{
let ditheredImage = try image?.applyErrorDifusion(withType: .floydSteinberg)
//or
//try image?.applyOrderedDither(withSize: .bayer8x8)
//or
//try image?.applyThreshold(withType: .fixed)
}catch{
//handle errors here
}
}
}
这些函数也具有更易于访问的参数,但为了简单起见,此示例没有。有关更多详细信息,请查看 Sample/DitherTester/DitherControl 并转到任何 retrivedDitheredImages。
GPU
import SwiftDithering
function yourFunction() {
let filter = OrderedDithering()
filter.setValuesForKeys([
"inputImage": inputCIImage,
"ditherType": ditherType.getCIFilterID(),
...
])
return filter?.outputImage
}
我建议在 iOS 中使用之前将 outputImage 转换为 CG,Mac 上的相应变体也是如此。
由于这是我第一次做类似的事情,很多代码都容易发生变化,我会尽量以最不具破坏性的方式进行,但我无法保证它在每个版本中都有效。
当前特性