NEW: Master Swift design patterns with my latest book! >>

How to use Core Graphics blend modes to draw a UIImage differently

Written by Paul Hudson    @twostraws

If you're rendering images using Core Graphics you should definitely try out some of the alternate blend modes that are available. If you've ever used Photoshop's blend modes these will be familiar: screen, luminosity, multiply and so on – these are all available right in Core Graphics.

To give you an idea what's possible, here's some code that takes two UIImages and draws them into one single image. The first image is drawn using normal rendering, and the second using .luminosity.

if let img = UIImage(named: "example"), let img2 = UIImage(named: "example2") {
    let rect = CGRect(x: 0, y: 0, width: img.size.width, height: img.size.height)
    let renderer = UIGraphicsImageRenderer(size: img.size)

    let result = renderer.image { ctx in
        // fill the background with white so that translucent colors get lighter
        UIColor.white.set()
        ctx.fill(rect)

        img.draw(in: rect, blendMode: .normal, alpha: 1)
        img2.draw(in: rect, blendMode: .luminosity, alpha: 1)
    }
}

How that looks depends on the source images you used – try drawing them the other way around to see what difference it makes, or try using .multiply rather than .luminosity.

If you're looking for a more advanced example, this function accepts an image and returns the same image with a rainbow effect to it. This is done by drawing six colored strips onto an image, then overlaying the original image using the blend mode .luminosity along with a slight alpha.

func addRainbow(to img: UIImage) -> UIImage {
    // create a CGRect representing the full size of our input iamge
    let rect = CGRect(x: 0, y: 0, width: img.size.width, height: img.size.height)

    // figure out the height of one section (there are six)
    let sectionHeight = img.size.height / 6

    // set up the colors – these are based on my trial and error
    let red = UIColor(red: 1, green: 0.5, blue: 0.5, alpha: 0.8)
    let orange = UIColor(red: 1, green: 0.7, blue: 0.35, alpha: 0.8)
    let yellow = UIColor(red: 1, green: 0.85, blue: 0.1, alpha: 0.65)
    let green = UIColor(red: 0, green: 0.7, blue: 0.2, alpha: 0.5)
    let blue = UIColor(red: 0, green: 0.35, blue: 0.7, alpha: 0.5)
    let purple = UIColor(red: 0.3, green: 0, blue: 0.5, alpha: 0.6)
    let colors = [red, orange, yellow, green, blue, purple]

    let renderer = UIGraphicsImageRenderer(size: img.size)
    let result = renderer.image { ctx in
        UIColor.white.set()
        ctx.fill(rect)

        // loop through all six colors
        for i in 0 ..< 6 {
            let color = colors[i]

            // figure out the rect for this section
            let rect = CGRect(x: 0, y: CGFloat(i) * sectionHeight, width: rect.width, height: sectionHeight)

            // draw it onto the context at the right place
            color.set()
            ctx.fill(rect)
        }

        // now draw our input image over using Luminosity mode, with a little bit of alpha to make it fainter
        img.draw(in: rect, blendMode: .luminosity, alpha: 0.6)
    }

    return result
}

Available from iOS 2.0

Did this solution work for you? Please pass it on!

Other people are reading…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

Learn Server-Side Swift now!

Take your Swift code to the server and become a full-stack developer with my latest book: Server-Side Swift!

Click here to visit the Hacking with Swift store >>