Roland
Roland

Reputation: 9711

Create multiple UIImage with plain color

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.

Upvotes: 2

Views: 723

Answers (4)

mxcl
mxcl

Reputation: 26893

I have created a CocoaPod that provides [UIImage imageWithColor:]:

pod 'UIImage+ImageWithColor'

Upvotes: 2

DrummerB
DrummerB

Reputation: 40211

You could just create a category on UIImage to return a new UIImage instance with a specific size and filled with a color:

+ (UIImage *)imageOfSize:(CGSize)size filledWithColor:(UIColor *)color {
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGRect rect = (CGRect){CGPointZero, size};
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

Upvotes: 3

debris
debris

Reputation: 503

check out this:

+ (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

optionally you can draw custom image from layer

+ (UIImage *)imageFromLayer:(CALayer *)layer{
    UIGraphicsBeginImageContext([layer frame].size);
    [layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return outputImage;
}

Upvotes: 1

Hussain Shabbir
Hussain Shabbir

Reputation: 15025

Try like this the same code:-

    UIImage *thumbnail =[UIImage imageNamed:@"yourImg.png"];
    CGSize itemSize = CGSizeMake(35, 35);
    UIGraphicsBeginImageContext(itemSize);
    CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
    [thumbnail drawInRect:imageRect];
    cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

Upvotes: 0

Related Questions