BLACK FRIDAY: Save 50% on all my Swift books and bundles! >>

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

Swift version: 5.10

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
}
Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Available from iOS 2.0

Similar solutions…

About the Swift Knowledge Base

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

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI 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 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 Beyond Code

Was this page useful? Let us know!

Average rating: 4.2/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.