iOS-UIImage与UIColor互转

UIImage与UIColor互转。

UIColor转UIImage:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
+(UIImage*)createImageWithColor:(UIColor *)color andSize:(CGSize)size
{
UIImage *img = nil;

CGRect rect = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, rect);

img = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return img;
}

UIImage转UIColor:

1
[UIColor colorWithPatternImage:image];