I have been looking for a way to draw an image programatically for a while now. I found a fairly good solution:
UIGraphicsBeginImageContextWithOptions(CGSizeMake(36, 36), NO, 0.0);
UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
But the downside of this is that in the same context I cannot create two or more images of different colours so I can use them as a background for a button for instance (for all three states). Yes, I know that I can set some resource PNG as the background of a UIButton
, but if I would want that I wouldn't ask this question.
I would really like to learn how to create images programatically and use them, with different colours as background, in the same context, or maybe created in another context but ported to where I need them.
EDIT: To clarify furthermore, take this piece of code:
UIButton *button = [UIButton alloc] init];
button.frame = CGRectMake(0.0, 0.0, 200, 30);
//this is pefect
button.backgroundColor = [UIColor whiteColor];
//but, what if I want to have the UIControlStateNormal / UIControlStateHighlighted / UIControlStateDisabled with different background colors ?
[button setBackgroundColor: [UIColor blackColor] forState: ?] // dont't think so, right ?
In the case above I'm forced to use an image, if I want the button to look different when pressed or disabled, of course, the background, because the title and the title colour can be easily changed.