Thursday 24 April 2014

Image Rendering Modes in iOS7


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.

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


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];


The following will illustrates the change in the UIImage with rendering mode and context.


UIImageRenderingModeAlwaysTemplate
tintColor - orangeColor
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");

}