定义一个类方法:
声明:
+ (UIImage *)imageFromColor:(UIColor *)color;
实现:
+ (UIImage *)imageFromColor:(UIColor *)color{
CGRect rect = CGRectMake(0, 0, 3, 3);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
CGRect rect = CGRectMake(0, 0, 3, 3);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}