Pregunta

Quiero algo de la siguiente manera

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

para crear una imagen con respecto a algún color.

cómo hacerlo en iPhone sdk.

¿Fue útil?

Solución

Puede dibujar el color en un CGContext y luego capturar una imagen de él:

- (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;
}

Otros consejos

Si solo está tratando de crear un rectángulo sólido de color, ¿por qué no hacer algo como

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

Y luego agregue la vista a cualquier subvista que desee que muestre el color sólido.

(Eso es, por supuesto, solo si eso es lo que estás tratando de lograr. Tal vez no lo estés)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top