/ iOS

UIImage Rendering Mode

最近调第三方代码的过程中发现,有一个 UIImage,无论将图片替换成什么,在页面上始终显示成蓝色。经过代码阅读,终于发现了猫腻:

[image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

简化版代码:

theImageView.image = [theImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[theImageView setTintColor:[UIColor redColor]];
theImageView.image = [theImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[theImageView setTintColor:[UIColor redColor]];

WatchKit 中的使用

In WatchKit for Apple Watch apps, you can set the tint color for a template image, as of Xcode 6.2 beta 4.

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. Unlike for an iPhone app, you cannot set the template rendering in code in the WatchKit Extension at present.
Set that image to be used in your WKInterfaceImage in interface builder for your app
Create an IBOutlet in your WKInterfaceController for the WKInterfaceImage called 'theImage'...
To then set the tint color in Swift:

theImage.setTintColor(UIColor.redColor())
To then set the tint color in 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. If you have not set a Global Tint, theImage will be tinted light blue by default when used as a template image.

参考

StackOverflow: How can I change image tintColor in iOS and WatchKit