Creating an empty UIImage of a given size and color programmatically

February 9, 2013 in iOS Snippets


+ (UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size

{

CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);

UIGraphicsBeginImageContext(rect.size);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [color CGColor]);

CGContextFillRect(context, rect);

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return image;

}