迅速-无法更改UIImage tintColor

问题描述

我有一个图案PNG图像,透明背景上有黑点,我需要以自定义颜色(例如黄色)更改点的颜色

我正在尝试在绘制颜色之前更改其颜色,但图像在透明状态下仍为黑色

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))

并且contextCGContext

任何建议都会有所帮助!

解决方法

正确的使用方法

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

代替