[iOS] 绘制纯色图片

2018-04-26  本文已影响38人  manajay
// 返回一张纯色图片
+ (UIImage *)lj_imageWithColor:(UIColor *)color rect:(CGRect)rect {

    // 开启位图上下文
    UIGraphicsBeginImageContext(rect.size);
    // 获取位图上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 使用color演示填充上下文
    CGContextSetFillColorWithColor(context, [color CGColor]);
    // 渲染上下文
    CGContextFillRect(context, rect);
    // 从上下文中获取图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    // 结束上下文
    UIGraphicsEndImageContext();
    
    return image;
}
上一篇 下一篇

猜你喜欢

热点阅读