iOS中 将 颜色转化成图片

时间:2023-03-08 22:30:54
定义一个类方法:
声明:
+ (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();
    UIGraphicsEndImageContext();
    return img;
}