On iOS, can you add more than one CIFilter
to a SKEffectsNode
?
CIFilterGenerator
seems like what I want but it isn't available on iOS.
I know you can use multiple filters on an image by passing the output of one as the input of the next, but that's not helpful if you want to affect non-image nodes.
Does this mean I have to create an artificial hierarchy of SKEffectNode
and add a filter to each of them, with my actual content at the very bottom? Is there a better way?
Where it's difficult or impossible to "chain" together multiple
CIFilter
calls to achieve the desired effect - maybe due to a class that has a single property, one way to overcome this is to do the following:CIFilter
, overriding everything you need to. This may includeattributes
,setValue(forKey:)
, and most importantly,outputImage
.CIFilterConstructor
, and create aregisterFilter()
method.For example, let's say you wish to combine a gaussian blur and then add a monochrome red tone to an image. At it's most basic you can do this:
If you wish to expose more attributes, you can simply add them to the
attributes
andsetValue(forKey:)
overrides along wist adding variables andsetDefaults
. Here I'm simply using the defaults.Now that you've chained your effect together into one custom filter, you can register it and use it:
To use this, just be sure to register the filter (I tend to put mine in
AppDelegate
if possible):From there, you can use
BlurThenColor
just like any otherCIFilter
. Instantiate it, usesetValue
, and calloutputImage
.Please note, this code will crash because of force-unwrapping of
inputImage
and/or typos. I'm sure you can make this more safe - but rest assured that I've tested this and it works. (I created this custom filter and replaced it in an app where the force-unwraps don't happen.)