swift - can't change UIImage tintColor

260 Views Asked by At

I have a pattern PNG image, with black dots on transparent background, and I need to change dot's color at custom colors, for example yellow

I'm trying to change tint color before drawing it, but image is still black on transparent

let templateImage = UIImage(named: "spray7")!.withTintColor(.yellow, renderingMode: .alwaysTemplate)
                                        
 let image = templateImage.cgImage
 context.draw(image!, in: CGRect(x: 0, y: 0, width: 8, height: 8))

and context is a CGContext

Any adviсe will be helpful!

1

There are 1 best solutions below

1
nastassia On BEST ANSWER

correct way is to use

let templateImage = UIImage(named: "spray7")!.tinted(with: .yellow)

instead