user3587674
user3587674

Reputation: 47

Creating UIImage programmatically from UIView

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?

Upvotes: 0

Views: 327

Answers (1)

Brams
Brams

Reputation: 674

+ (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;
}

Upvotes: 1

Related Questions