我的目标是改变代码中的系统UIImage
的颜色。我声明该图像与
let defaultImage = UIImage(systemName: "person.circle.fill")
但是当我改变它的色调时:
let grayDefaultImage = defaultImage?.withTintColor(.gray)
grayDefaultImage
仍然显示标准的蓝色。接下来,我使用一个分机
//MARK: -UIImage extension
extension UIImage {
/// @Todo: this blurs the image for some reason
func imageWithColor(color: UIColor) -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
color.setFill()
let context = UIGraphicsGetCurrentContext()
context?.translateBy(x: 0, y: self.size.height)
context?.scaleBy(x: 1.0, y: -1.0)
context?.setBlendMode(CGBlendMode.normal)
let rect = CGRect(origin: .zero, size: CGSize(width: self.size.width, height: self.size.height))
context?.clip(to: rect, mask: self.cgImage!)
context?.fill(rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
}
图像以非常“像素化”或“低分辨率”的形式呈现。
发布于 2020-04-23 02:55:35
:) say:The new image uses the same rendering mode as the original image.
因此,如果原始图像的渲染方式是.alwaysTemplate
,则意味着图像会忽略其颜色信息。您需要将模式设置为.alwaysOriginal
defaultImage?.withTintColor(.gray, renderingMode: .alwaysOriginal)
https://stackoverflow.com/questions/61376879
复制相似问题