根据颜色生成图片

// 根据颜色生成一张尺寸为1*1的相同颜色图片

+ (UIImage *)imageWithColor:(UIColor *)color;

#import "UIImage+Image.h"

@implementation UIImage (Image)

+ (UIImage *)imageWithColor:(UIColor *)color

{

    // 描述矩形

    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);

    

    // 开启位图上下文

    UIGraphicsBeginImageContext(rect.size);

    // 获取位图上下文

    CGContextRef context = UIGraphicsGetCurrentContext();

    // 使用color演示填充上下文

    CGContextSetFillColorWithColor(context, [color CGColor]);

    // 渲染上下文

    CGContextFillRect(context, rect);

    // 从上下文中获取图片

    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();

    // 结束上下文

    UIGraphicsEndImageContext();

    

    return theImage;

}

原文地址:https://www.cnblogs.com/liaolijun/p/6807138.html