简体   繁体   English

如何设置图像的色调颜色

[英]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)
}
}

I have updated my question according to possible answer Here is my code for the changing the icon color.我已根据可能的答案更新了我的问题 这是我更改图标颜色的代码。 In some reason my user icon is not fully filling its color when i change the tint color.出于某种原因,当我更改色调颜色时,我的用户图标没有完全填充其颜色。

I had added 2 more methods to your extension of UIImage for tint an Image and added some changes in your overlayed method我在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? Tinted 方法的内容来自这个问题How can I color a UIImage in Swift? answer provided by @HR @HR 提供的答案

Use It用它

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

UPDATED更新

Result with the last update上次更新的结果

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM