Reputation: 172
I know how to create a UIImage programatically, with the following code:
CGRect rect = CGRectMake(0.0f, 0.0f, 300.0f, 60.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
What I want to do is, to fill it with multiple colors. like the following
Upvotes: 3
Views: 1151
Reputation: 1942
You know how to paint one rectangle, just extrapolate for painting multiple rectangles as below. Usse your own colors. I have used inbuilt color, if need any custom colors then set the color using RGB value.
CGRect rect = CGRectMake(0.0f, 0.0f, 300.0f, 60.0f);
NSArray *colorArray = [NSArray arrayWithObjects:[UIColor redColor],[UIColor yellowColor],[UIColor greenColor],[UIColor brownColor],
[UIColor lightGrayColor],nil];
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
for (int i = 0; i < colorArray.count; i++)
{
CGRect smallRect = CGRectMake((300.0f /colorArray.count) * i,0.0f,(300.0f /colorArray.count) ,60.0f);
CGContextSetFillColorWithColor(context, [colorArray[i] CGColor]);
CGContextFillRect(context, smallRect);
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Upvotes: 9