根据颜色生成图片

// 根据颜色生成一张尺寸为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;

}

 

posted on 2017-05-04 14:57  廖利君  阅读(465)  评论(0编辑  收藏  举报