MacOS 和 Swift 3 带有 CIAffineClamp 过滤器

MacOS and Swift 3 with CIAffineClamp filter

我需要使用 CIAffineClamp 来扩展图像并防止高斯模糊模糊图像的边缘。我有以下代码在 Swift 2:

中工作
let transform = CGAffineTransformIdentity
let clampFilter = CIFilter(name: "CIAffineClamp")
clampFilter.setValue(inputImage, forKey: "inputImage")
clampFilter.setValue(NSValue(CGAffineTransform: transform), forKey: "inputTransform")

在Swift 3 CGAffineTransformIdentity中重命名为CGAffineTransform.identity。我的代码可以编译,但是我在控制台中收到以下错误消息:

[CIAffineClamp inputTransfom] is not a valid object.

Apple 网站上的文档指出 MacOS 上的 inputTransform 参数采用 属性类型为 CIAttributeTypeTransform.NSAffineTransform 对象,但我'我不确定如何使用它。

如有任何帮助,我们将不胜感激。

似乎 NSAffineTransform 有一个初始化器 NSAffineTransform.init(transform:) 需要 AffineTransform.

请试试这个:

let transform = AffineTransform.identity
let clampFilter = CIFilter(name: "CIAffineClamp")!
clampFilter.setValue(inputImage, forKey: "inputImage")
clampFilter.setValue(NSAffineTransform(transform: transform), forKey: "inputTransform")

或者最后一行可以是:

clampFilter.setValue(transform, forKey: "inputTransform")

NSAffineTransform

Important

The Swift overlay to the Foundation framework provides the AffineTransform structure, which bridges to the NSAffineTransform class. The AffineTransform value type offers the same functionality as the NSAffineTransform reference type, and the two can be used interchangeably in Swift code that interacts with Objective-C APIs. This behavior is similar to how Swift bridges standard string, numeric, and collection types to their corresponding Foundation classes.