我创建了一个小项目来查询 iOS 设备并 (1) 列出所有可用的过滤器并 (2) 列出有关每个输入属性的所有内容。这个项目可以在here找到。
相关代码:
var ciFilterList = CIFilter.filterNames(inCategories: nil)
这一行创建了一个包含所有可用过滤器的[String]。如果您只希望使用“CICategoryBlur”类别的所有可用过滤器,请将nil 替换为它。
print("=======")
print("List of available filters")
print("-------")
for ciFilterName in ciFilterList {
print(ciFilterName)
}
print("-------")
print("Total: " + String(ciFilterList.count))
相当不言自明。当我在运行 iOS 12.0.1 的 iPad mini 上运行它时,列出了 207 个过滤器。注意:我从未在 macOS 上尝试过,但由于它确实不使用UIKit,我相信它会起作用。
let filterName = "CIZoomBlur"
let filter = CIFilter(name: filterName)
print("=======")
print("Filter Name: " + filterName)
let inputKeys = filter?.inputKeys
if inputKeys?.count == 0 {
print("-------")
print("No input attributes.")
} else {
for inputKey in inputKeys! {
print("-------")
print("Input Key: " + inputKey)
if let attribute = filter?.attributes[inputKey] as? [String: AnyObject],
let attributeClass = attribute[kCIAttributeClass] as? String,
let attributeDisplayName = attribute["CIAttributeDisplayName"] as? String,
let attributeDescription = attribute[kCIAttributeDescription] as? String {
print("Display name: " + attributeDisplayName)
print("Description: " + attributeDescription)
print("Attrbute type: " + attributeClass)
switch attributeClass {
case "NSNumber":
let minimumValue = (attribute[kCIAttributeSliderMin] as! NSNumber).floatValue
let maximumValue = (attribute[kCIAttributeSliderMax] as! NSNumber).floatValue
let defaultValue = (attribute[kCIAttributeDefault] as! NSNumber).floatValue
print("Default value: " + String(defaultValue))
print("Minimum value: " + String(minimumValue))
print("Maximum value: " + String(maximumValue))
case "CIColor":
let defaultValue = attribute[kCIAttributeDefault] as! CIColor
print(defaultValue)
case "CIVector":
let defaultValue = attribute[kCIAttributeDefault] as! CIVector
print(defaultValue)
default:
// if you wish, just dump the variable attribute to look at everything!
print("No code to parse an attribute of type: " + attributeClass)
break
}
}
}
}
}
print("=======")
再次,相当不言自明。我正在编写的应用程序仅适用于使用单个 CIImage 的过滤器,并且属性限制为 NSNumber、CIColor 和 CIVector,所以事情将属于默认部分switch 声明。但是,它应该让你开始!如果您想查看“原始”版本,只需查看 attribute 变量即可。
最后,我推荐一下由 Simon Gladman 开发的名为 Filterpedia 的东西。它是一款 iPad 应用程序(仅限横向),可让您尝试几乎所有可用的过滤器以及所有具有默认/最大值/最小值的属性。不过要注意两件事。 (1) 它是用 Swift 2 编写的,但它是一个 Swift 4 fork here。 (2) 还有很多使用自定义CIKernels的自定义过滤器。