App开发中经常会遇到的一种情况是同一张图片不同颜色,每次设计都要重新切图给你(返回箭头,错误提示)对设计来说是工作量的增加了无效的工作量,而且资源文件无形之中也有增加,先来看一张图片:
上面的三张图片都是一个图片,其他两张只是设计了一下image渲染的模式,先来看一下UIImage渲染的三种模式:
typedef NS_ENUM(NSInteger, UIImageRenderingMode) {
UIImageRenderingModeAutomatic, // Use the default rendering mode for the context where the image is used
UIImageRenderingModeAlwaysOriginal, // Always draw the original image, without treating it as a template
UIImageRenderingModeAlwaysTemplate, // Always draw the image as a template image, ignoring its color information
} NS_ENUM_AVAILABLE_IOS(7_0);
UIImageRenderingModeAutomatic:默认模式,根据上下文渲染
UIImageRenderingModeAlwaysOriginal:始终根据原始图片渲染
UIImageRenderingModeAlwaysTemplate:模板渲染,根据tintColor渲染;
三种图片实现代码:
UIImage *image = [UIImage imageNamed:@"error"];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.firstImgView.image =image;
self.firstImgView.tintColor = [UIColor yellowColor];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
self.secondImgView.image = image;
self.secondImgView.tintColor = [UIColor redColor];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
self.lastImgView.image = image;
self.lastImgView.tintColor = [UIColor greenColor];