gpt4 book ai didi

swift - 在 swift3 中不使用 Uiimageview 更改 UIImage 颜色

转载 作者:行者123 更新时间:2023-11-28 15:44:50 26 4
gpt4 key购买 nike

我想绘制像星星一样的图像图案,可以为此更改颜色。我可以在不使用 ImageView 的情况下更改图像本身的颜色吗?我看到很多答案改变了 UIImageview 的色调,但效果不适用于 UIImage

最佳答案

谢谢大家的回答,但我得到了解决方案。

func changePatternImageColor() {
patternImage = UIImage(named: "pattern_star.png")! //make sure to reinitialize the original image here every time you change the color
UIGraphicsBeginImageContext(patternImage.size)
let context = UIGraphicsGetCurrentContext()
let color = UIColor(red: self.red, green: self.green, blue: self.blue, alpha: self.opacity)
color.setFill()
context?.translateBy(x: 0, y: patternImage.size.height)
context?.scaleBy(x: 1.0, y: -1.0)
//set the blend mode to color burn, and the original image
context?.setBlendMode(CGBlendMode.exclusion) //this does the magic.
let rect = CGRect(x: 0, y: 0, width: patternImage.size.height, height: patternImage.size.height)
context?.draw(patternImage.cgImage!, in: rect)
//set the mask that matches the shape of the image, then draw color burn a colored rectangle
context?.clip(to: rect, mask: patternImage.cgImage!)
context?.addRect(rect)
context?.drawPath(using: CGPathDrawingMode.fill)
patternImage = UIGraphicsGetImageFromCurrentImageContext()!
image_ref = patternImage.cgImage
UIGraphicsEndImageContext()
}

patternImage 是在 Assets 中添加的图像。附言添加到 Assets 中的图案图像应该是黑色

关于swift - 在 swift3 中不使用 Uiimageview 更改 UIImage 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43272362/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com