如何在 Swift 中更改 UIImage 的颜色
How to change the color of a UIImage in Swift
我在 swift 中,我正在尝试生成一个接受 UIImage 和 UIColor 以及 returns 重新着色的 UIImage 的函数
我没有使用 UIImageView,这些只是我打算用作图标的 UIImage。有什么好的实现方法吗?
如果您使用 PNG 图像(我认为是因为图标)- 只需使用:
let originalImage = UIImage(named: "iconName")
let tintedImage = originalImage?.withRenderingMode(.alwaysTemplate)
yourButton.setImage(tintedImage, forState: .normal)
yourButton.tintColor = UIColor.blue //change color of icon
edit/update:
对于 iOS10+ 我们可以使用 UIGraphicsImageRenderer:
Xcode 11 • Swift 5.1
extension UIImage {
func tinted(with color: UIColor, isOpaque: Bool = false) -> UIImage? {
let format = imageRendererFormat
format.opaque = isOpaque
return UIGraphicsImageRenderer(size: size, format: format).image { _ in
color.set()
withRenderingMode(.alwaysTemplate).draw(at: .zero)
}
}
}
游乐场测试
let camera = UIImage(data: try! Data(contentsOf: URL(string: "https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-camera-128.png")!))!
let redCamera = camera.tinted(with: .red)
原回答
您可以使用 UIGraphicsBeginImageContextWithOptions 开始图像上下文,设置所需的颜色并使用图像的方法 func draw(in rect: CGRect)
使用渲染模式 .alwaysTemplate
在其上绘制图标图像:
extension UIImage {
func tinted(with color: UIColor) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(size, false, scale)
defer { UIGraphicsEndImageContext() }
color.set()
withRenderingMode(.alwaysTemplate)
.draw(in: CGRect(origin: .zero, size: size))
return UIGraphicsGetImageFromCurrentImageContext()
}
}
iOS 13 岁及以上 (Swift 5.1):
声明(see Docs)
func withTintColor(_ color: UIColor) -> UIImage
用法:
yourUIImage.withTintColor(color: UIColor)
我在 swift 中,我正在尝试生成一个接受 UIImage 和 UIColor 以及 returns 重新着色的 UIImage 的函数
我没有使用 UIImageView,这些只是我打算用作图标的 UIImage。有什么好的实现方法吗?
如果您使用 PNG 图像(我认为是因为图标)- 只需使用:
let originalImage = UIImage(named: "iconName")
let tintedImage = originalImage?.withRenderingMode(.alwaysTemplate)
yourButton.setImage(tintedImage, forState: .normal)
yourButton.tintColor = UIColor.blue //change color of icon
edit/update:
对于 iOS10+ 我们可以使用 UIGraphicsImageRenderer:
Xcode 11 • Swift 5.1
extension UIImage {
func tinted(with color: UIColor, isOpaque: Bool = false) -> UIImage? {
let format = imageRendererFormat
format.opaque = isOpaque
return UIGraphicsImageRenderer(size: size, format: format).image { _ in
color.set()
withRenderingMode(.alwaysTemplate).draw(at: .zero)
}
}
}
游乐场测试
let camera = UIImage(data: try! Data(contentsOf: URL(string: "https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-camera-128.png")!))!
let redCamera = camera.tinted(with: .red)
原回答
您可以使用 UIGraphicsBeginImageContextWithOptions 开始图像上下文,设置所需的颜色并使用图像的方法 func draw(in rect: CGRect)
使用渲染模式 .alwaysTemplate
在其上绘制图标图像:
extension UIImage {
func tinted(with color: UIColor) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(size, false, scale)
defer { UIGraphicsEndImageContext() }
color.set()
withRenderingMode(.alwaysTemplate)
.draw(in: CGRect(origin: .zero, size: size))
return UIGraphicsGetImageFromCurrentImageContext()
}
}
iOS 13 岁及以上 (Swift 5.1):
声明(see Docs)
func withTintColor(_ color: UIColor) -> UIImage
用法:
yourUIImage.withTintColor(color: UIColor)