如何在iPhone sdk中获取彩色图像

rkb*_*rkb 7 iphone uiimage

我想要的东西如下

UIImage *solid = [UIImage imageWithColor:[UIColor darkGrayColor]];
Run Code Online (Sandbox Code Playgroud)

创建关于某种颜色的图像.

如何在iPhone sdk中做到这一点.

Lou*_*arg 21

您可以将颜色绘制到CGContext中,然后从中捕获图像:

- (UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size {
  //Create a context of the appropriate size
  UIGraphicsBeginImageContext(size);
  CGContextRef currentContext = UIGraphicsGetCurrentContext();

  //Build a rect of appropriate size at origin 0,0
  CGRect fillRect = CGRectMake(0,0,size.width,size.height);

  //Set the fill color
  CGContextSetFillColorWithColor(currentContext, color.CGColor);

  //Fill the color
  CGContextFillRect(currentContext, fillRect);

  //Snap the picture and close the context
  UIImage *retval = UIGraphicsGetImageFromCurrentImageContext(void);
  UIGraphicsEndImageContext();

  return retval;
}
Run Code Online (Sandbox Code Playgroud)