import Foundation
// MARK: - Extensions UIImage
public extension UIImage {
/// Tint, Colorize image with given tint color
/// This is similar to Photoshop's "Color" layer blend mode
/// This is perfect for non-greyscale source images, and images that
/// have both highlights and shadows that should be preserved<br><br>
/// white will stay white and black will stay black as the lightness of
/// the image is preserved
///
/// - Parameter TintColor: Tint color
/// - Returns: Tinted image
public func tintImage(with fillColor: UIColor) -> UIImage {
return modifiedImage { context, rect in
// draw black background - workaround to preserve color of partially transparent pixels
context.setBlendMode(.normal)
UIColor.black.setFill()
context.fill(rect)
// draw original image
context.setBlendMode(.normal)
context.draw(cgImage!, in: rect)
// tint image (loosing alpha) - the luminosity of the original image is preserved
context.setBlendMode(.color)
fillColor.setFill()
context.fill(rect)
// mask by alpha values of original image
context.setBlendMode(.destinationIn)
context.draw(context.makeImage()!, in: rect)
}
}
/// Modified Image Context, apply modification on image
///
/// - Parameter draw: (CGContext, CGRect) -> ())
/// - Returns: UIImage
fileprivate func modifiedImage(_ draw: (CGContext, CGRect) -> ()) -> UIImage {
// using scale correctly preserves retina images
UIGraphicsBeginImageContextWithOptions(size, false, scale)
let context: CGContext! = UIGraphicsGetCurrentContext()
assert(context != nil)
// correctly rotate image
context.translateBy(x: 0, y: size.height)
context.scaleBy(x: 1.0, y: -1.0)
let rect = CGRect(x: 0.0, y: 0.0, width: size.width, height: size.height)
draw(context, rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
}
extension UIImageView {
// make template image with tint color
var templateImage: Bool {
set {
if newValue, let image = self.image {
let newImage = image.withRenderingMode(.alwaysTemplate)
self.image = newImage
}
} get {
return false
}
}
}
0
我必须使用 extension 在Swift中执行此操作 .
我以为我会分享我是怎么做到的:
extension UIImage {
func imageWithColor(color1: UIColor) -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
color1.setFill()
let context = UIGraphicsGetCurrentContext() as CGContextRef
CGContextTranslateCTM(context, 0, self.size.height)
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetBlendMode(context, CGBlendMode.Normal)
let rect = CGRectMake(0, 0, self.size.width, self.size.height) as CGRect
CGContextClipToMask(context, rect, self.CGImage)
CGContextFillRect(context, rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext() as UIImage
UIGraphicsEndImageContext()
return newImage
}
}
18 回答
用于着色UIButton的图像
这是我的UIImage扩展,您可以直接对图像使用changeTintColor函数 .
像这样的用法示例
您可以使用更改
changeColor
功能来更改图像颜色斯威夫特4
更改 UIImage SVG / PDF 的色调,适用于 image with unique color :
更改 UIImageView 的色调,适用于 image with unique color :
为 picture 更改 UIImage 的色调,使用:
试试这个
http://robots.thoughtbot.com/designing-for-ios-blending-modes
要么
profileImageView.image = theImageView.image!.withRenderingMode(.alwaysTemplate)
profileImageView.tintColor = UIColor.green
要么
首先选择图像资源中的特定图像,然后选择红色为模板而不是默认值,然后选择该写入行 . profileImageView.tintColor = UIColor.green
Swift 3
扩展答案的版本来自 fuzz现在我使用这个基于Duncan Babbage响应的方法:
这是一个应该做的技巧
所以你会这样做:
iOS
从InterfaceBuilder,set templateImage param in keyPath执行此操作的解决方案,并选择您从IB着色
}
我必须使用
extension
在Swift中执行此操作 .我以为我会分享我是怎么做到的:
用法:
theImageView.image = theImageView.image.imageWithColor(UIColor.redColor())
Swift 4
用法:
theImageView.image = theImageView.image?.imageWithColor(color1: UIColor.red)
Take benefit of Extension in Swift :-
可以从代码和Interface Builder中使用的子类:
在故事板和图像资产 . 你也可以改变这两个:
将渲染模式更新为模板图像
更新视图中的色调颜色 .
随着Swift
iOS
对于iOS应用程序,在Swift 3或4中:
斯威夫特2:
同时,现代的Objective-C解决方案是:
Watchkit
在WatchKit for Apple Watch应用程序中,您可以设置tint color for a template image .
您必须将图像添加到WatchKit应用程序中的资产目录,并将图像集设置为在属性检查器中呈现为模板图像 . 与iPhone应用程序不同,您目前无法在WatchKit Extension中的代码中设置模板渲染 .
设置要在应用程序的界面构建器中的WKInterfaceImage中使用的图像
在WKInterfaceController中为名为'theImage'的WKInterfaceImage创建一个IBOutlet ...
然后在Swift 3或4中设置色调颜色:
斯威夫特2:
然后在Objective-C中设置色调颜色:
如果您使用模板图像并且不应用色调颜色,则将应用WatchKit应用程序的全局色调 . 如果尚未设置全局色调,则
theImage
在用作模板图像时将默认为浅蓝色 .如果有人关心没有
UIImageView
的解决方案:为了迅速3目的
theImageView.image = theImageView.image!.withRenderingMode(.alwaysTemplate) theImageView.tintColor = UIColor.red