iOS 根据颜色创建个图片

UIImage的分类,可用于动态改变navigetionBar的背景图片颜色,

示例

  // 修改navigationBar的背景图片
  [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor purpleColor]] forBarMetrics:UIBarMetricsDefault];

    // 修改navigationBar的线条的图片

    [self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:[UIColor whiteColor]]];



//
根据颜色创建图片 尺寸为1*1 + (UIImage *)imageWithColor:(UIColor *)color;
 1 + (UIImage *)imageWithColor:(UIColor *)color
 2 {
 3     // 描述矩形
 4     CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
 5     
 6     // 开启位图上下文
 7     UIGraphicsBeginImageContext(rect.size);
 8     // 获取位图上下文
 9     CGContextRef context = UIGraphicsGetCurrentContext();
10     // 使用color演示填充上下文
11     CGContextSetFillColorWithColor(context, [color CGColor]);
12     // 渲染上下文
13     CGContextFillRect(context, rect);
14     // 从上下文中获取图片
15     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
16     // 结束上下文
17     UIGraphicsEndImageContext();
18     
19     return image;
20 }

 

posted on 2015-08-19 10:50  airy99  阅读(1276)  评论(0编辑  收藏  举报