问题
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
AffineTransformstructure, which bridges to theNSAffineTransformclass. TheAffineTransformvalue type offers the same functionality as theNSAffineTransformreference 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