Centurion Centurion - 1 year ago 93
Objective-C Question

How to create UIImage with vertical gradient using "from-color" and "to-color"

How to create new image just with gradient colors, using "from-color" and "to-color"?

Answer Source

A simpler answer, using a CAGradientLayer.

CGSize size = CGSizeMake(width, height);
CAGradientLayer *layer = [CAGradientLayer layer];
layer.frame = CGRectMake(0, 0, size.width, size.height);
layer.colors = @[(__bridge id)[UIColor blackColor].CGColor,   // start color
                 (__bridge id)[UIColor whiteColor].CGColor]; // end color

[layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download