SALE ENDS TODAY: Save 50% on all Swift books and bundles! >>

How to use Core Image filters the type-safe way

iOS 13 fixed this once and for all

Paul Hudson       @twostraws

iOS 13 introduced new type-safe initializers for Core Image filters that allow us to use them much more easily – and without having to hope that our code works at runtime. Previously much of Core Image was stringly typed, meaning that we would create filters using strings, which in turn meant that Swift couldn't be sure that when we asked for a value to be set that it would actually exist. iOS 13 replaces that with a much better implementation, and you'll never want to go back to the old way.

This functionality is enabled by new protocols that define exactly what each filter can do. For example, CILinearGradient has two points and two colors, while CIBarsSwipeTransition has an angle, a width, and a bar offset. So, while we aren't getting concrete types back, the protocols at least mean we have guaranteed access to all the properties we need.

Start by adding an import to bring in all the new types:

import CoreImage
import CoreImage.CIFilterBuiltins

Now you can go ahead and create filters using static methods on CIFilter. For example, CIFilter.qrCodeGenerator() sends back a filter to generate QR codes. Once you have the type you want, you'll find properties you can set specific to that filter, which is a huge improvement over the old calls to setValue(_forKey:).

For example, we could we create a Gaussian blur effect using CIFilter.gaussianBlur(), set its input image and radius, then read out the result:

let context = CIContext(options: nil)
let blur = CIFilter.gaussianBlur()
blur.inputImage = CIImage(image: exampleImage)
blur.radius = 30

if let output = blur.outputImage {
    if let cgimg = context.createCGImage(output, from: output.extent) {
        let processedImage = UIImage(cgImage: cgimg)
        // use your blurred image here
    }
}

SPONSORED Instabug helps you identify and resolve severe crashes quickly. You can retrace in-app events and know exactly which line of code caused the crash along with environment details, network logs, repro steps, and the session profiler. Ask more questions or keep users up-to-date with in-app replies straight from your dashboard. Instabug takes data privacy seriously, so no one sees your data but you! See more detailed features comparison and try Instabug's crash reporting SDK for free.

Save 50% on all books and bundles

The biggest ever Hacking with Swift sale is now on, letting you save 50% on all books and bundles. Learn something new with Swift and enjoy great savings while the sale lasts!

Click here to save 50% in our Black Friday sale!

BUY OUR BOOKS
Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

About the author

Paul Hudson is the creator of Hacking with Swift, the most comprehensive series of Swift books in the world. He's also the editor of Swift Developer News, the maintainer of the Swift Knowledge Base, and a speaker at Swift events around the world. If you're curious you can learn more here.

Was this page useful? Let us know!

Average rating: 3.0/5