Apple introduces a new property UIImageRenderingMode with UIImage from iOS7. There are three types of rendering modes which can apply to an image which are given below.
typedef enum : NSInteger {
UIImageRenderingModeAutomatic,
UIImageRenderingModeAlwaysOriginal,
UIImageRenderingModeAlwaysTemplate,
} UIImageRenderingMode;
UIImageRenderingModeAutomatic
This is the default rendering mode.
UIImageRenderingModeAlwaysOriginal
By using this mode , the image will be rendered with out any modifications to the image property. Which is exactly equivalent to reading the image from disk file and rendering in the screen.
UIImageRenderingModeAlwaysTemplate
This mode will ignore the colour information of image. And apply the colour according to the way the context is defined. Context is the tintColor of the superview of UIImageView where the image is added.
2. Really suitable for applications supporting more that one theme as colour. A single line of code is enough to change the colour of all the images and there by changing the theme.
The following lines of code will change the colour of the image which is added in an UIViewController.
0r
rOneImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 30, 292, 297)];
The following will illustrates the change in the UIImage with rendering mode and context.
with green-color Original image with orange-color
The following code will create a UIImageView with UIImage and add to UIViewController's view as given below.
And the button action is given below.
For tracking changes to tintColor , subclass UIView and override the following method.
For more info please refer:-
UIImage Class Reference - Apple Developer
UIView Class Reference - Apple Developer
UIImageRenderingModeAutomatic
This is the default rendering mode.
UIImageRenderingModeAlwaysOriginal
By using this mode , the image will be rendered with out any modifications to the image property. Which is exactly equivalent to reading the image from disk file and rendering in the screen.
UIImageRenderingModeAlwaysTemplate
This mode will ignore the colour information of image. And apply the colour according to the way the context is defined. Context is the tintColor of the superview of UIImageView where the image is added.
Advantages using UIImageRenderingModeAlwaysTemplate
1. The number of image resources can be reduced by reusing the same kind of image file. This will help to the application bundle size.2. Really suitable for applications supporting more that one theme as colour. A single line of code is enough to change the colour of all the images and there by changing the theme.
The following lines of code will change the colour of the image which is added in an UIViewController.
self.view.tintColor = [UIColor greenColor];
0r
[[[UIApplication sharedApplication] keyWindow] setTintColor:[UIColor orangeColor]];
Creating a UIImage with rendering mode
UIImage *image1 = [UIImage imageNamed:@"circular_arrow"];
image1 = [image1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
rOneImageView.image = image1;
[self.view addSubview:rOneImageView];
The following will illustrates the change in the UIImage with rendering mode and context.
UIImageRenderingModeAlwaysTemplate tintColor - greenColor |
UIImageRenderingModeAlwaysOriginal |
with green-color Original image with orange-color
How it works
The following code will create a UIImageView with UIImage and add to UIViewController's view as given below.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
rOneImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 30, 292, 297)];
UIImage *image1 = [UIImage imageNamed:@"circular_arrow"];
image1 = [image1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
rOneImageView.image = image1;
[self.view addSubview:rOneImageView];
}
And the button action is given below.
- (IBAction)changeColor:(id)sender{
self.view.tintColor = [UIColor greenColor];
}
For tracking changes to tintColor , subclass UIView and override the following method.
- (void)tintColorDidChange{
NSLog(@"tintColorDidChange");
}
For more info please refer:-
UIImage Class Reference - Apple Developer
UIView Class Reference - Apple Developer