本文翻譯自: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)