Scale to fill for UIColor pattern UIImage

Sometimes we need to scale to fill our images to fit in certain image view. This code snippet easly do this. I definitely recommend to you use UIImage category for this. So that you don’t repeat same code again and again.

 
+ (UIImage *)fitImageWithView:(UIView *)view imageName:(NSString *)imageName {
    
    if(imageName.length < 1) {
        [NSException raise:@"There is no image name." format:@"Image name is : %@", imageName];
    }
    
    UIImage *img = nil;
    
    UIGraphicsBeginImageContext(view.frame.size);
    [[UIImage imageNamed:imageName] drawInRect:view.bounds];
    img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return img;
}
Advertisements