Created
September 27, 2013 01:25
-
-
Save mattt/6722980 to your computer and use it in GitHub Desktop.
Create a UIImage swatch of a color with a specified size.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
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.
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
As used in the FormatterKit demo: