如何将以下ObjC代码的最后一行转换为Swift:

CGAffineTransform transform = CGAffineTransformIdentity;
CIFilter *clampFilter = [CIFilter filterWithName:@"CIAffineClamp"];

[clampFilter setValue:[NSValue valueWithBytes:&transform objCType:@encode(CGAffineTransform)] forKey:@"inputTransform"];

下一行
clampFilter.setValue(transform, forKey: "inputTransform")

产生
Type 'CGAffineTransform' does not conform to protocol 'AnyObject'

最佳答案

NSValue具有采用CGAffineTransform参数的构造函数:

let transform = CGAffineTransformIdentity
let clampFilter = CIFilter(name: "CIAffineClamp")

let val = NSValue(CGAffineTransform: transform)
clampFilter.setValue(val, forKey: "inputTransform")

的更新Swift 3:
let transform = CGAffineTransform.identity
let clampFilter = CIFilter(name: "CIAffineClamp")!
let val = NSValue(cgAffineTransform: transform)
clampFilter.setValue(val, forKey: "inputTransform")

关于ios - 在Swift中设置CIFilter,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26259721/

10-10 20:45