How to invert image colors in Swift
Apple provides a library for manipulating images called Core Image. Core Image allows you to do near real time manipulation and processing of still and video images. Today we’ll use Core Image to invert the colors of an image.
To get started we need an image. A quick google search lead me to one that I liked. Go ahead and save it to your computer:
Now, create a new Xcode project and add the image to it by dragging it into your project. Make sure to check “copy items if needed”.
To make this a bit more fun, I decided to make the image take up the full screen of the app. The code was pretty simple:
Here we make our imageView a global variable, so we can access it throughout the class then:
- We can get the screen size using UIScreen.mainScreen().bounds.
- We get our image (make sure it actually exists).
- Stick the image into the image view.
- Finally, add the image view as a subview.
I want the inversion to happen when the image is tapped, so we’ll quickly add a tap gesture recognizer to the image view:
Notice that we enable user interaction by setting userInteractionEnabled on the image view to true.
Now, when you tap on the image you should see a message in the console which says “image tapped”.
Now comes what you all came here for. Inverting the colors of the image. Here are the basic steps we’ll follow:
- Create a CIImage from the image within our image view.
- Create a filter using CIFilter
- Set the image as the input image to the filter.
- Save the output of the filter as a new image.
- Finally, set the new image to the image view
Here’s the final code:
You’ll notice that we used CIColorInvert as the filter and we had to use filter.setValue to set the image to the kCIInputImageKey.
Run your app and tap on the image. It inverts!
Now, if you tap on the image twice you’ll get an error and a crash. This is because the image returned from the filter doesn’t have a bitmap. There are ways to sort this out, but they are beyond the scope of this post. I’ll leave it as an exercise for the reader ;)