How to auto draw an UIImage with frame and color?
Asked Answered
R

1

0

I try something to create an UIImage with a frame and color but don't know what the correct way.

UIImage *aImage = [UIImage imageWithCIImage:[CIImage imageWithColor:[CIColor colorWithCGColor:[UIColor redColor].CGColor]]];

Is the above line correct? How to create image size? Please help!

Ragouzis answered 17/3, 2013 at 13:33 Comment(0)
S
7

make a UIImage category.

such as UIImage(Color)

+ (UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size
{
    UIGraphicsBeginImageContextWithOptions(size, YES, 0);

    [color set];
    UIBezierPath * path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, size.width, size.height)];
    [path fill];

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

    return image;
}

How to use :

#import "UIImage + Color"

UIImage *image = [UIImage imageWithColor:[UIColor redColor] andSize:yourSize];
Slugabed answered 17/3, 2013 at 13:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.