2014-10-08 146 views
5

設置CIFilter一個人如何翻譯以下ObjC代碼的斯威夫特最後一行:在斯威夫特

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' 

回答

13

NSValue有采用CGAffineTransform參數的構造函數:

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

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

更新斯威夫特3:

let transform = CGAffineTransform.identity 
let clampFilter = CIFilter(name: "CIAffineClamp")! 
let val = NSValue(cgAffineTransform: transform) 
clampFilter.setValue(val, forKey: "inputTransform") 
+0

它在Swift 3.1中崩潰。你可以發佈任何CIAffineClamp或CIAffineTile的例子嗎? – ChanWarde 2017-07-10 09:41:19

+0

@ChanWarde:請檢查更新是否適合您。 – 2017-07-10 09:52:19

+0

hello @Martin R,即使只有TileEffect中的「CIKaleidoscope」正在爲我工​​作,它也無法正常工作。所有其他過濾器都返回零圖像。我沒有找到任何示例,問題或參考。你可以請添加一些代碼。或者我應該爲它添加一個新問題? – ChanWarde 2017-07-10 12:44:26

1

這可能會做。

var transform = CGAffineTransformIdentity; 
var clampFilter = CIFilter(name: "CIAffineClamp") 
var value = NSValue(CGAffineTransform: transform) 
clampFilter.setValue(value, forKey: "inputTransform") 
+1

除非你專門在未來改變價值,否則你應該總是使用'let'。 – Fogmeister 2014-10-08 15:03:03