Showing posts with label UIImage. Show all posts
Showing posts with label UIImage. Show all posts

Sunday, 30 July 2017

Swift In Practice: Using Asset Catalog Identifiers


The usual way of handling things


Usually , we will be accessing image from asset catalogs as given below. For example, we have an asset catalog which contains three images.



Common Implementation


Whats wrong with the above usage


We will be using this hard coded image name informations in lot more places in our code. And any typo mistake will create bugs and the developer may unaware of this, since compiler will not capture typo errors in hard coded strings.

So how can we approach these problems in a more robust way and make our code more clean.

As a solution, we can use some global string constants. But is that adequate? the answer is No. See the following code.



It is clear, that UIImage designated initialiser can take any string constants , which will not show any errors during complication but will throw exception at run time. So , this can introduce some problems.

So its better to use strongly typed strings for asset catalog identifiers by making use of application specific enums. Also if we add a convenience initialiser to UIImage with a parameter named as "assetIdentifier" or something else according to your taste it will be perfect, Please refer the implementation given below.

Asset Catalog enums and UIImage convenience initialiser implementation




Asset Catalog Identifier Usage


Advantages

1. Typo errors in image names can be avoided since we are using enums. Which will result in compilation errors incase of Typo errors.

2. UIImage initialisers always returned with an image object.

3. Its easy identify unused images in your code by commenting an enum type. Which will not produce any compilation issue if the respective image is unused.

4. The class or file can be used a centralised source of your resources where you can find all of your image names.

References:

1. WWDC Video - Swift In Practice




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");

}