Core Image Tutorial in iOS8 with Swift

Core Image provides a mechanism for manipulating images and videos. Core Image includes a range of filters such as cropping, gradients, color effects and blurring. In this tutorial we will set a mono effect on a coloured image. This tutorial is built in iOS 8.1 with Xcode 6.2

Open Xcode and create a new Single View Application. For product name, use IOS8SwiftCoreImageTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.

Go to the Storyboard and drag an Image View to the main View. Select the Image View and go to the Size inspector and fill in the following values.

Select the Image View again and drag from inside the Image View just to to right will still in the Image View. Hold down the Ctrl key and select the "Equal Widths" and Equal Heights" Auto Layout options.

Select the Image View again and drag from the Image View to the top of the view. Hold down the Ctrl key and select "Top space to Top Layout Guide" and "Center Horizontally in Container" options.

The Storyboard will look like this.

Select the Assistant Editor and make sure the ViewController.swift is visible. Ctrl and drag from the Image View to the ViewController class  and create the following Outlet.

Download the image, and drag it to the project. Select the image to see how the original will look like.

Go to the ViewController.swift file and change the viewDidLoad method

override func viewDidLoad() {
    // 1
    let image = UIImage(named: "dog.jpg")
    let originalImage = CIImage(image: image)
    // 2
    var filter = CIFilter(name: "CIPhotoEffectMono")
    filter.setValue(originalImage, forKey: kCIInputImageKey)
    // 3
    var outputImage = filter.outputImage
    var newImage = UIImage(CIImage: outputImage)
    imageView.image = newImage
  1. The image is loaded and assigned to the image variable. Since Core Image works on CIImage object the UIImage will be converted to a CIImage.
  2. a CIFilter object is initialised with the CIPhotoEffectMono filter. The filter is first set to default and then assigned to the CIImage.
  3. The filter is applied to the outputImage and is then converted back to an UIImage object. Finally the filtered image is assigned to the Image View. 

Build and Run the project, the filtered image is displayed. 

You can download the source code of the IOS8SwiftCoreImageTutorial at the ioscreator repository on Github.