如何设置图像的色调颜色

How to set tint color of an Image

let tintedImage = #imageLiteral(resourceName: "user")
pictureImageView?.image = mainCircleImage.overlayed(with: tintedImage,color: UIColor.orange)

extension UIImage {
func overlayed(with overlay: UIImage,color:UIColor) -> UIImage? {
    defer {
        UIGraphicsEndImageContext()
    }
    UIGraphicsBeginImageContextWithOptions(size, false, scale)
    self.draw(in: CGRect(origin: CGPoint.zero, size: size))
    let tintedOverlay = overlay.tintedImageWithColor(color: color)
    tintedOverlay.draw(in: CGRect(origin: CGPoint.zero, size: size))
    if let image = UIGraphicsGetImageFromCurrentImageContext() {
        return image
    }
    return nil
}

func tint(color: UIColor, blendMode: CGBlendMode) -> UIImage
{
    let drawRect = CGRect(x: 0.0, y: 0.0, width: size.width, height: size.height)
    UIGraphicsBeginImageContextWithOptions(size, false, scale)
    let context = UIGraphicsGetCurrentContext()
    context!.scaleBy(x: 1.0, y: -1.0)
    context!.translateBy(x: 0.0, y: -self.size.height)
    context!.clip(to: drawRect, mask: cgImage!)
    color.setFill()
    UIRectFill(drawRect)
    draw(in: drawRect, blendMode: blendMode, alpha: 1.0)
    let tintedImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return tintedImage!
}

func tintedImageWithColor(color: UIColor) -> UIImage
{
    return self.tint(color: color, blendMode: CGBlendMode.multiply)
}
}

我已经根据可能的答案更新了我的问题 这是我更改图标颜色的代码。由于某些原因,当我更改色调时,我的用户图标没有完全填充它的颜色。

我已经为您的 UIImage 扩展添加了 2 个方法来为图像着色,并在您的 overlayed 方法中添加了一些更改

extension UIImage {
    func overlayed(with overlay: UIImage,color:UIColor) -> UIImage? {
        defer {
            UIGraphicsEndImageContext()
        }
        UIGraphicsBeginImageContextWithOptions(size, false, scale)
        self.draw(in: CGRect(origin: CGPoint.zero, size: size))
        let tintedOverlay = overlay.tinted(color: color)
        tintedOverlay.draw(in: CGRect(origin: CGPoint.zero, size: size), blendMode: .multiply, alpha: 1.0)
        if let image = UIGraphicsGetImageFromCurrentImageContext() {
            return image
        }
        return nil
    }

    func tinted(color: UIColor) -> UIImage
    {
        UIGraphicsBeginImageContextWithOptions(self.size, false, UIScreen.main.scale)
        let context = UIGraphicsGetCurrentContext()

        color.setFill()

        context!.translateBy(x: 0, y: self.size.height)
        context!.scaleBy(x: 1.0, y: -1.0)

        context!.setBlendMode(CGBlendMode.colorBurn)
        let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
        context!.draw(self.cgImage!, in: rect)

        context!.setBlendMode(CGBlendMode.sourceIn)
        context!.addRect(rect)
        context!.drawPath(using: CGPathDrawingMode.fill)

        let coloredImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        return coloredImage!
    }
}

The content of Tinted method is from this question How can I color a UIImage in Swift? answer provided by @HR

使用它

    let mainCircleImage = UIImage(named: "actions_menu_edit")?
    let tintedImage = UIImage(named: "actions_menu_add")
    pictureImageView?.image = mainCircleImage?.overlayed(with: tintedImage!,color: UIColor.red)

已更新

最后更新的结果