如何使用框架和颜色自动绘制UIImage?

时间:2013-03-17 13:33:38

标签: ios uiimage core-image

我尝试用框架和颜色创建一个UIImage,但不知道正确的方法。

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

上述行是否正确?如何创建图像大小?请帮忙!

1 个答案:

答案 0 :(得分:7)

制作UIImage类别。

,例如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;
}

使用方法:

#import "UIImage + Color"

UIImage *image = [UIImage imageWithColor:[UIColor redColor] andSize:yourSize];