Question

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?

Was it helpful?

Solution

+ (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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top