0

Is there way to create a UIImage or a UIImageView from a UIView with background color. My purpose is to get this color in the image. If it is possible, how can I do it?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • So you want to create a view with a background colour just to get an image of that colour? (you only need a context for the image and draw the colour into that) What are you then going to use the image for? – Wain Apr 30 '14 at 13:09
  • you need to explain a little further about what you need exactly? Its quite absurd based on the details you provided – Khawar Ali Apr 30 '14 at 13:10
  • I think what you are asking is how to create a UIImage that's a specific color. Is this correct? – John Riselvato Apr 30 '14 at 13:11

1 Answers1

1
+ (UIImage *)imageWithColor:(UIColor *)color andRect:(CGRect)rect {
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}
Brams
  • 584
  • 4
  • 9
  • Straight copy from http://stackoverflow.com/a/993159/525576 a 1px by 1px color is not going to be useful for him. Maybe pass the cgrect instead of predefine it. – John Riselvato Apr 30 '14 at 13:14
  • I edited it so it can be passed to the function. I used the response from the link you passed for one of my current project ... so I'm sharing it now. – Brams Apr 30 '14 at 13:20