UIImage from color

You ever needed the UIImage from the color? I had a small requirement where i needed that. Here is the small simple snippet that will get the UIImage from any colour.

- (UIImage *) getImageWithColor: (UIColor *)color
{
    UIImage *image;
    
    CGFloat width = 640, height = 640;
    
	UIGraphicsBeginImageContext(CGSizeMake(width, height));
    
	CGContextRef context = UIGraphicsGetCurrentContext();
    
	UIGraphicsPushContext(context);
    
	[image drawInRect:CGRectMake(0, 0, width, height)];
    
    CGRect backRect = CGRectMake(0, 0,
                                 width,
                                 height);
    
    [color setFill];
    CGContextFillRect(context, backRect);
    
	UIGraphicsPopContext();
    
	UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
    
	UIGraphicsEndImageContext();
    
    return outputImage;
}

Be the first to leave a comment. Don’t be shy.

Join the Discussion

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>