How to auto draw an UIImage with frame and color?

一笑奈何 提交于 2019-12-13 04:24:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!