Core Image and Pro-Active Laziness

Laziness is a beautiful thing. Thanks to the non-existent motivation to do repetitive, boring and tedious tasks, we as a population grow and develop alternative ways to accomplish the same things, but in a new, more time efficient and productive manner. One such task is working with Core Image.

Do you remember when you were first learning Core Image? Perhaps it was many years ago, but I’m sure you’ll remember thinking “What’s up with all these -[CIFilter setValue:forKey:] calls”? Quite frankly, this is my least favorite bit of the Core Image API, and I’m sure that many of you feel the same way. While I understand the need for this API (ex: each filter has different and sometimes unique properties that aren’t shared among other filters), it is a massive pain in ass to use and introduces a swarm of possibilities for error. Misspelled filter names and lack of compile-time type-checking are just the top two. There are also issues with usability. You can’t just Command+Click a property to see the type it expects and the default value provided by the filter. Wouldn’t it be nice if Xcode could autocomplete filter names? Perhaps even warn of incompatible types at compile time and offer an easy reference for property type and default values?

As you may have guessed by now, this is exactly what this article is about. Tired of using -[CIFilter setValue:forKey:] I wrote a simple generator that would query all available CIFilter classes on the platform and generate a header and implementation file with a subclass for each filter type. In every subclass you’ll also find properties associated with that specific filter and you can use them to set values without having to call setValue:forKey:.

You can provide your own file name for these files to match your project and even customize the subclass prefix to match. It’s worth noting that on iOS the generated files will be saved to your app’s sandbox and you’ll need to access the Document directory with an app like DiskAid or iExplorer to retrieve the files and add them to your project. Alternatively, you can use the pre-generated set of files provided in the sample project.

To use the generated file, simply add it your project and import it where appropriate:

1

#import "DBFilterSubclasses.h"

I really hope that this makes your life a little bit better while using the Core Image framework. It is extremely powerful and capable framework, and now its even better with Xcode’s autocompletion, compile-time type checking and no more setValue:forKey:.