iOS中通过UIColor生成UIImage

司马腾
2023-12-01

title: iOS中通过UIColor生成UIImage
date: 2012-05-06 12:45:14
tags:

  • UIColor
  • UIImage
    categories:
  • iOS
  • iOS-Tips

创建一个UIImage的Category添加一下方法:

+ (instancetype)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = [UIImage new];
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}
 类似资料: