问题
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!
回答1:
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];
来源:https://stackoverflow.com/questions/15461309/how-to-auto-draw-an-uiimage-with-frame-and-color