本文翻译自:How can I change image tintColor in iOS and WatchKit
I have an UIImageView called "theImageView", with UIImage in a single color (transparent background) just like the left black heart below.
我有一个名为“ theImageView”的UIImageView,UIImage具有单色(透明背景),就像下面的黑色左心一样。How can I change the tint color of this image programmatically in iOS 7 or above, as per the tint method used in the iOS 7+ Navigation Bar icons?
如何按照iOS 7+导航栏图标中使用的着色方法,在iOS 7或更高版本中以编程方式更改此图像的着色颜色?Can this method also work in WatchKit for an Apple Watch app?
此方法也可以在Apple Watch应用程序的WatchKit中使用吗?#1楼
参考:https://stackoom.com/question/1IsFh/如何在iOS和WatchKit中更改图像tintColor
#2楼
Here's a category that should do the trick
这是应该解决的问题@interface UIImage(Overlay)
@end
@implementation UIImage(Overlay)
- (UIImage *)imageWithColor:(UIColor *)color1
{
UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, self.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
CGContextClipToMask(context, rect, self.CGImage);
[color1 setFill];
CGContextFillRect(context, rect);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
@end
so you would do:
所以你会做:theImageView.image = [theImageView.image imageWithColor:[UIColor redColor]];
#3楼
I had to do this in Swift using an
extension
.
我必须在Swift中使用extension
执行此操作。 I thought I'd share how I did it:
我以为我会分享我的做法: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
}
}
Usage:
用法:theImageView.image = theImageView.image.imageWithColor(UIColor.redColor())
Swift 4
斯威夫特4extension UIImage {
func imageWithColor(color1: UIColor) -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
color1.setFill()
let context = UIGraphicsGetCurrentContext()
context?.translateBy(x: 0, y: self.size.height)
context?.scaleBy(x: 1.0, y: -1.0)
context?.setBlendMode(CGBlendMode.normal)
let rect = CGRect(origin: .zero, size: CGSize(width: self.size.width, height: self.size.height))
context?.clip(to: rect, mask: self.cgImage!)
context?.fill(rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
}
Usage:
用法:theImageView.image = theImageView.image?.imageWithColor(color1: UIColor.red)
#4楼
Try this
尝试这个http://robots.thoughtbot.com/designing-for-ios-blending-modes
http://robots.thoughtbot.com/designing-for-ios-blending-modesor
要么- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 50)];
label.numberOfLines = 0;
label.font = [UIFont systemFontOfSize:13];
label.text = @"These checkmarks use the same gray checkmark image with a tintColor applied to the image view";
[self.view addSubview:label];
[self _createImageViewAtY:100 color:[UIColor purpleColor]];
}
- (void)_createImageViewAtY:(int)y color:(UIColor *)color {
UIImage *image = [[UIImage imageNamed:@"gray checkmark.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect frame = imageView.frame;
frame.origin.x = 100;
frame.origin.y = y;
imageView.frame = frame;
if (color)
imageView.tintColor = color;
[self.view addSubview:imageView];
}
#5楼
iOS
iOS版For an iOS app, in Swift 3 or 4:
对于iOS应用,在Swift 3或4中:theImageView.image = theImageView.image?.withRenderingMode(.alwaysTemplate)
theImageView.tintColor = UIColor.red
Swift 2:
斯威夫特2:theImageView.image = theImageView.image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
theImageView.tintColor = UIColor.redColor()
Meanwhile, the modern Objective-C solution is:
同时,现代的Objective-C解决方案是:theImageView.image = [theImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[theImageView setTintColor:[UIColor redColor]];
Watchkit
WatchkitIn WatchKit for Apple Watch apps, you can set the tint color for a template image .
在Apple Watch应用程序的WatchKit中,您可以设置模板图像的色调颜色 。- You must add your image to an Asset Catalog in your WatchKit App, and set the image set to be rendered as a Template Image in the Attributes Inspector. 您必须将图像添加到WatchKit App的资产目录中,并在Attributes Inspector中将图像集设置为呈现为模板图像。 Unlike for an iPhone app, you cannot set the template rendering in code in the WatchKit Extension at present. 与iPhone应用程序不同,当前您无法在WatchKit Extension中的代码中设置模板渲染。
- Set that image to be used in your WKInterfaceImage in interface builder for your app 设置要在应用程序的界面生成器中的WKInterfaceImage中使用的图像
- Create an IBOutlet in your WKInterfaceController for the WKInterfaceImage called 'theImage'... 在WKInterfaceController中为名为'theImage'的WKInterfaceImage创建一个IBOutlet ...
To then set the tint color in Swift 3 or 4:
然后在Swift 3或4中设置色调颜色:theImage.setTintColor(UIColor.red)
Swift 2:
斯威夫特2:theImage.setTintColor(UIColor.redColor())
To then set the tint color in Objective-C:
然后在Objective-C中设置色调颜色:[self.theImage setTintColor:[UIColor redColor]];
If you use a template image and do not apply a tint colour, the Global Tint for your WatchKit app will be applied.
如果您使用模板图像并且不应用色调颜色,则将应用WatchKit应用程序的“全局色调”。If you have not set a Global Tint,
theImage
will be tinted light blue by default when used as a template image.
如果尚未设置全局色调,则在用作模板图像时,默认情况下,theImage
将淡蓝色。 #6楼
With Swift
与斯威夫特let commentImageView = UIImageView(frame: CGRectMake(100, 100, 100, 100))
commentImageView.image = UIImage(named: "myimage.png")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
commentImageView.tintColor = UIColor.blackColor()
addSubview(commentImageView)