美文网首页
iOS swift如何修改图片的颜色

iOS swift如何修改图片的颜色

作者: 可乐小子 | 来源:发表于2024-02-25 11:35 被阅读0次

    extension UIImage {

    func imageWithTintColor(_ color: UIColor, blendMode: CGBlendMode = .normal) -> UIImage? {
        
        guard self.size.height > 0 && self.size.width > 0 else {
            return nil
        }
        
        let rect = CGRect(origin: .zero, size: self.size)
        UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
        
        guard let context = UIGraphicsGetCurrentContext() else { return nil }
        
        context.translateBy(x: 0, y: self.size.height)
        context.scaleBy(x: 1.0, y: -1.0)
        context.setBlendMode(blendMode)
        context.clip(to: rect, mask: self.cgImage!)
        color.setFill()
        context.fill(rect)
        
        let coloredImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        return coloredImage
      
    }
    }

    相关文章

      网友评论

          本文标题:iOS swift如何修改图片的颜色

          本文链接:https://www.haomeiwen.com/subject/obkaadtx.html