UIColor和UIImage转换

况鸿雪
2023-12-01

UIColor 转UIImage

- (UIImage*) createImageWithColor: (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 *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

调用方法:

[Button setBackgroundImage:[self createImageWithColor:[UIColor redColor]] forState:UIControlStateDisabled];

UIImage转UIColor

[UIColor colorWithPatternImage:[UIImage imageNamed:@"EmailBackground.png"]]

转载自 点击打开链接

 类似资料: