苹果引入了一个新的属性UIImageRenderingMode,有三种类型的渲染模式如下:
typedef enum : NSInteger {
UIImageRenderingModeAutomatic,//这是默认的渲染模式
UIImageRenderingModeAlwaysOriginal, //通过使用这种模式,图像会呈现出任何修改图像属性
UIImageRenderingModeAlwaysTemplate, //这种模式将会忽略图像的颜色信息。并应用颜色根据上下文定义的方式。
} UIImageRenderingMode;
1.使用UIImageRenderingModeAlwaysTemplate的优点:
a. 图像资源的数量可以减少通过重用相同的图像文件。这将有助于应用程序包减压;
b.支持更多主题的颜色。一行代码就能改变所有的图片的颜色来改变主题;
使用以下代码来改变view的图片颜色:
self.view.tintColor = [UIColor greenColor];
//或者
[[[UIApplication sharedApplication] keyWindow] setTintColor:[UIColor orangeColor]];
2.UIImageRenderinMode来创建Image:
UIImageView *OneImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 30, 292, 297)];
UIImage *image1 = [UIImage imageNamed:@"circular_arrow"];
image1 = [image1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
OneImageView.image = image1;
[self.view addSubview:OneImageView];
3.工作原理:
直接see下面代码:
- (void)viewDidLoad
- {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//创建UIImageView
UIImageView *imageView= [[UIImageView alloc] initWithFrame:CGRectMake(10, 30, 292, 297)];
UIImage *image1 = [UIImage imageNamed:@"circular_arrow"];
image1 = [image1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
imageView.image = image1;
[self.view addSubview:imageView];
}
//action
- (IBAction)changeColor:(id)sender{
self.view.tintColor = [UIColor greenColor];
}
//修改tintcolor
- (void)tintColorDidChange{
BJLog(@"tintColorDidChange");
}
参考:
网友评论