swiftuiimagecore-graphicscgcontextcgcontextdrawimage

swift - can't change UIImage tintColor


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!


Solution

  • correct way is to use

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

    instead