Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the color of a UIImage in Swift

I'm in swift and I'm trying to generate a function that takes in a UIImage and a UIColor and returns a UIImage recolored

I'm not working with a UIImageView, these are simply UIImages that I intend to use as icons. Is there any good way to implement this?

like image 714
TJBlack31 Avatar asked Nov 25 '17 23:11

TJBlack31


People also ask

How do I change the color of an Imagematically in Swift?

The absolute simplest way to change colors of images (or icons in this case) is to use the SF Symbols where applicaple. This is a set of symbols Apple provides that can easily be used in your own app.

How do I change the UIImage color in Objective C?

extension UIImage{ static func multiplyImageByConstantColor(image:UIImage,color:UIColor) -> UIImage{ let backgroundSize = image. size UIGraphicsBeginImageContext(backgroundSize) guard let ctx = UIGraphicsGetCurrentContext() else {return image} var backgroundRect=CGRect() backgroundRect.

How do I change the color of the SF symbol?

SF Custom symbols can be colored in 4 distinct color modes : Monochrome, Hierarchical, Palette, Multicolor. To access the colors modes open the SF Symbols and click on the segmented control with the Pain Brush icon and chose a different Rendering.


3 Answers

edit/update:

For iOS10+ we can use UIGraphicsImageRenderer:

Xcode 11 • Swift 5.1

extension UIImage {
    func tinted(with color: UIColor, isOpaque: Bool = false) -> UIImage? {
        let format = imageRendererFormat
        format.opaque = isOpaque
        return UIGraphicsImageRenderer(size: size, format: format).image { _ in
            color.set()
            withRenderingMode(.alwaysTemplate).draw(at: .zero) 
        }
    }
}

Playground Testing

let camera = UIImage(data: try! Data(contentsOf: URL(string: "https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-camera-128.png")!))!
let redCamera = camera.tinted(with: .red)

original answer

You can use UIGraphicsBeginImageContextWithOptions to begin an image context, set the desired color and use image's method func draw(in rect: CGRect) to draw your icon image using rendering mode .alwaysTemplate on it:

extension UIImage {
    func tinted(with color: UIColor) -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(size, false, scale)
        defer { UIGraphicsEndImageContext() }
        color.set()
        withRenderingMode(.alwaysTemplate)
            .draw(in: CGRect(origin: .zero, size: size))
        return UIGraphicsGetImageFromCurrentImageContext()
    }
}

enter image description here

like image 199
Leo Dabus Avatar answered Oct 18 '22 03:10

Leo Dabus


If you use PNG images (as i think because of icons) - just use:

let originalImage = UIImage(named: "iconName")
let tintedImage = originalImage?.withRenderingMode(.alwaysTemplate)
yourButton.setImage(tintedImage, forState: .normal)
yourButton.tintColor = UIColor.blue //change color of icon
like image 30
derdida Avatar answered Oct 18 '22 01:10

derdida


iOS 13 and above (Swift 5.1):

Declaration (see Docs)

func withTintColor(_ color: UIColor) -> UIImage

usage:

yourUIImage.withTintColor(color: UIColor)
like image 2
Paul Avatar answered Oct 18 '22 03:10

Paul