Skip to content

Instantly share code, notes, and snippets.

@mattt
Created September 27, 2013 01:25
Show Gist options
  • Save mattt/6722980 to your computer and use it in GitHub Desktop.
Save mattt/6722980 to your computer and use it in GitHub Desktop.
Create a UIImage swatch of a color with a specified size.
static UIImage * UIImageForSwatchOfColorWithSize(UIColor *color, CGSize size) {
UIImage *image = nil;
CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
{
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(c, [color CGColor]);
CGContextFillRect(c, rect);
image = UIGraphicsGetImageFromCurrentImageContext();
}
UIGraphicsEndImageContext();
return image;
}
@mattt
Copy link
Author

mattt commented Sep 27, 2013

As used in the FormatterKit demo:

Color

@mattt
Copy link
Author

mattt commented Sep 27, 2013

And for anyone asking why I didn't just set the background color of a view, it seemed easier to just take advantage of the automatic layout UITableViewCell provides when an image is specified.

@mrakowski111
Copy link

Why did you choose to make this a static method (instead of a class method for example)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment