MacOS and Swift 3 with CIAffineClamp filter

江枫思渺然 提交于 2019-12-25 08:29:48

问题


I need to use CIAffineClamp in order to extend the image and prevent Gaussian Blur from blurring out edges of the image. I have the following code working in Swift 2:

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

In Swift 3 CGAffineTransformIdentity was renamed to CGAffineTransform.identity. My code compiles however I get the following error message in the console:

[CIAffineClamp inputTransfom] is not a valid object.

Documentation on Apple's websites states that inputTransform parameter on MacOS takes an NSAffineTransform object whose attribute type is CIAttributeTypeTransform. but I'm unsure how to use it.

Any help would be appreciated.


回答1:


Seems NSAffineTransform has an initializer NSAffineTransform.init(transform:) which takes AffineTransform.

Please try this:

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

Or the last line can be:

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.



来源:https://stackoverflow.com/questions/39941090/macos-and-swift-3-with-ciaffineclamp-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!