Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a color image in iPhone sdk

Tags:

iphone

uiimage

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.

like image 709
rkb Avatar asked Jul 31 '09 17:07

rkb


People also ask

How do I get the exact color on my Iphone?

Use the eyedropper tool to pick an exact color from an image on the screen. It can be useful if you want to match a color on the current document or one from another file. It's also handy if you see a color you like and want to save it for later.

What is UIImage?

An object that manages image data in your app.

How do I color in Swiftui?

For adding a color set, you go to the Assets Folder -> Click "+" button on bottom left corner -> "Add Color Set". You can customize the color for a particular color scheme or can have the same color for both modes.


2 Answers

You can draw the color into a CGContext and then capture an image from it:

- (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;
}
like image 175
Louis Gerbarg Avatar answered Oct 19 '22 07:10

Louis Gerbarg


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)

like image 2
jbrennan Avatar answered Oct 19 '22 09:10

jbrennan