iOS 颜色生成image

来源:互联网 发布:python电子书 编辑:程序博客网 时间:2024/05/16 12:37

UIButton设置背景图片时,是需要一张图片才可以。但是我又想直接通过UIColor来设置,那怎么办?

该方法是可以将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;      }  

可以封装成一个工具类,因为是类方法,直接调用即可

0 0