Image processing in Swift

Photo by Ilya Pavlov / Unsplash

Apple gives us very powerfull tools for processing images in Swift on all platforms. Framework CoreImage provides many filters, effects, and blending options with lighting fast performance. It even can automatically enhance a photo!


CIImage is not really an image. This class has all information about the image, but it is not rendered. Think about it as instructions how to create the output.


CIFilter is responsible for image processing, Core Image have many built options, but we can write custom filters. Each filter have its own input parameters, but developer documentation is not very effective source of information. I found this app and this site. It contains all CIFilter related informations with examples of usage and output.


CIContext is where magic happens, it is responsible for image analysis and rendering. It can even detect faces with CIDetector. Everything with performance in mind.

The code

I'll use Playgrounds for running code, you can check it out in iOS app or Mac app, or Playgrounds. Everything goes :)

The test subject

For purpouse of demonstration, and cuteness (very important) I'll use picture of my dogs:

Cute doggos:)

Creating CIImage

Obtaining CIImage object is simple, it can be produced from UIImage or directly from file. I added my dogs to resources and loaded via URL. I'm creating CIContext, it will be needed at the end.

import CoreImage

let context = CIContext()
let url = Bundle.main.url(forResource:"doggos", withExtension:"jpeg")!
let ciImage = CIImage(contentsOf: url)

Creating filters

I want image to be black and white and make it look like instant photo. To make colors dissapear, I'll use CIColorControls, and bring saturation to 0. For instant photo effect there is built in option - CIPhotoEffectInstant. Every filter has input and input, but different parameters. Check them on website above.

Black and white

let blackAndWhiteFilter = CIFilter(name: "CIColorControls")!
blackAndWhiteFilter.setValue(ciImage, forKey: "inputImage")
blackAndWhiteFilter.setValue(0, forKey: kCIInputSaturationKey)

Effect? Fantastic!

Black and white cutties!

Instantify B&W image

We can chain filters. To do that, use output of first filter and pass as input to second one. How awesome is that? Core Image will produce combined effect by its own, no additional code required!

let instantFilter = CIFilter(name: "CIPhotoEffectInstant")!
instantFilter.setValue(blackAndWhiteFilter.outputImage!, forKey: "inputImage")

Now final image:

Output image

I saved images as files. But if you want display them on iPhone screen, use convinient method for creating UIImage:

let finalUIImage = UIImage(ciImage: finalImage)


With a little help of Core Image framework we can create stunning visuals and breathtaking effects. Feel free to explore more filters and see what you can achieve with your imagination and creativity!

Artur Gruchała

Artur Gruchała

I started learning iOS development when Swift was introduced. Since then I've tried Xamarin, Flutter, and React Native. Nothing is better than native code:)