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?
-
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– Wain2014-04-30 13:09:33 +00:00Commented Apr 30, 2014 at 13:09
-
you need to explain a little further about what you need exactly? Its quite absurd based on the details you providedKhawar Ali– Khawar Ali2014-04-30 13:10:34 +00:00Commented Apr 30, 2014 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– John Riselvato2014-04-30 13:11:26 +00:00Commented Apr 30, 2014 at 13:11
Add a comment
|
1 Answer
+ (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;
}
2 Comments
John Riselvato
Straight copy from 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.
Brams
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.