ios – 在Swift中设置CIFilter

前端之家收集整理的这篇文章主要介绍了ios – 在Swift中设置CIFilter前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何将以下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相关文章