对于imageWithRenderingMode方法的两种参数的使用解释

1.现在我们拿UITabbarController举例,来验证imageWithRenderingMode:的两种不同传参方式引起的最终效果,看如下代码:

UITabBarController *tabbarCon = [[UITabBarController alloc]init];
tabbarCon.tabBar.barTintColor = [UIColor colorWithHex:0xEFF0DB alpha:0.5];
tabbarCon.delegate = self;

MainViewController  *firstView = [[MainViewController alloc]init];
UINavigationController *navigation1 = [[UINavigationController alloc]initWithRootViewController:firstView];

ContactViewController *secondView = [[ContactViewController alloc]init];
UINavigationController *navigation2 = [[UINavigationController alloc]initWithRootViewController:secondView];

2.这里创建了两个tabbarItem:

UITabBarItem *item1 = [[UITabBarItem alloc]initWithTitle:@"首页" image:[UIImage imageNamed:@"tabbar_home_f"] selectedImage:[UIImage imageNamed:@"tabbar_home"]];
navigation1.tabBarItem = item1;

UITabBarItem *item2 = [[UITabBarItem alloc]initWithTitle:@"联系方式" image:[UIImage imageNamed:@"tabbar_contact_us"] selectedImage:[UIImage imageNamed:@"tabbar_contact_us_f"]];
navigation2.tabBarItem = item2;

3.如果此时我们不做其他操作,直接将两个navigationController添加到tabbarController上面:

tabbarCon.viewControllers = @[navigation1,navigation2];

[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor lightGrayColor]} forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor orangeColor]} forState:UIControlStateSelected];

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = tabbarCon;
[self.window makeKeyAndVisible];
return YES;

4.接下来看效果,图一:
对于imageWithRenderingMode方法的两种参数的使用解释_第1张图片

5.而我的原始图片是这样的,图二:

这里写图片描述

6.可见此时- (instancetype)initWithTitle:(nullable NSString )title image:(nullable UIImage )image selectedImage:(nullable UIImage *)selectedImage方法只是将图片依据图片模版渲染了出来,然而并没有渲染颜色。那么我们再在初始化后加上如下代码:

navigation1.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_home"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
navigation1.tabBarItem.image = [[UIImage imageNamed:@"tabbar_home_f"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

navigation2.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_contact_us"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
navigation2.tabBarItem.image = [[UIImage imageNamed:@"tabbar_contact_us_f"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

7.此时我们再看结果,图三:

对于imageWithRenderingMode方法的两种参数的使用解释_第2张图片

8.好了,此时图片的颜色加上了,不再只是个模型了。

9.之后我们再将上面的UIImageRenderingModeAlwaysOriginal修改为UIImageRenderingModeAlwaysTemplate,再次运行,我们发现结果又如图一那样了。由此我们得出,- (instancetype)initWithTitle:(nullable NSString )title image:(nullable UIImage )image selectedImage:(nullable UIImage *)selectedImage只是将原图模版不加颜色的渲染出来。

再查看源码中的注释:
对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,意味着将图像绘制为模板图像,忽略颜色的渲染。

你可能感兴趣的:(iOS)