Setting up CIFilter in Swift
Asked Answered
P

2

7

How does one translate the last line of the following ObjC code to Swift:

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

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

The following line

clampFilter.setValue(transform, forKey: "inputTransform")

produces

Type 'CGAffineTransform' does not conform to protocol 'AnyObject'
Peptonize answered 8/10, 2014 at 14:41 Comment(0)
P
18

NSValue has a constructor that takes a CGAffineTransform argument:

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

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

Update for Swift 3:

let transform = CGAffineTransform.identity
let clampFilter = CIFilter(name: "CIAffineClamp")!
let val = NSValue(cgAffineTransform: transform)
clampFilter.setValue(val, forKey: "inputTransform")
Preventer answered 8/10, 2014 at 14:58 Comment(4)
It's crashing in Swift 3.1. Can you post any example of CIAffineClamp or CIAffineTile.Comenius
@ChanWarde: Please check if the update works for you.Preventer
hello @Martin R, it's not working even only "CIKaleidoscope" in TileEffect is working for me. All other filters are returning nil image. I don't find any example, question or reference for it. Can you please add some code. Or I should add a new question for it??Comenius
@ChanWarde: I would suggest to ask a new question. Don't forget to add the code that you tried and a clear description where it fails.Preventer
A
2

This might do.

var transform = CGAffineTransformIdentity;
var clampFilter = CIFilter(name: "CIAffineClamp")
var value = NSValue(CGAffineTransform: transform)
clampFilter.setValue(value, forKey: "inputTransform")
Arch answered 8/10, 2014 at 14:58 Comment(1)
You should always use let in swift unless you are specifically going to change the value in the future.Speed

© 2022 - 2024 — McMap. All rights reserved.