簡體   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)
}
}

我已根據可能的答案更新了我的問題 這是我更改圖標顏色的代碼。 出於某種原因,當我更改色調顏色時,我的用戶圖標沒有完全填充其顏色。

我在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!
    }
}

Tinted 方法的內容來自這個問題How can I color a UIImage in Swift? @HR 提供的答案

用它

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

更新

上次更新的結果

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM