简体   繁体   English

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

[英]How to get a color image in iPhone sdk

I want something as follows 我想要的东西如下

UIImage *solid = [UIImage imageWithColor:[UIColor darkGrayColor]];

to create an image with respect to some color. 创建关于某种颜色的图像。

how to do it in iPhone sdk. 如何在iPhone sdk中做到这一点。

You can draw the color into a CGContext and then capture an image from it: 您可以将颜色绘制到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;
}

If you're just trying to create a solid rectangle of colour why not just do something like 如果您只是想创建一个实心的矩形颜色,为什么不只是做类似的事情

UIView *solid = [[UIView alloc] initWithFrame:someFrame];
solid.backgroundColor = [UIColor greyColor];

And then add the view to whatever subview you want to show the solid colour. 然后将视图添加到要显示纯色的子视图中。

(That is, of course only if that's what you're trying to achieve. Maybe you aren't) (当然,只有这是你想要实现的目标。也许你不是)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM