Skip to content

Instantly share code, notes, and snippets.

@sag333ar
Last active December 29, 2015 07:38
Show Gist options
  • Select an option

  • Save sag333ar/7637021 to your computer and use it in GitHub Desktop.

Select an option

Save sag333ar/7637021 to your computer and use it in GitHub Desktop.
Create an image using color and size supplied in arguments.
// get image color & size as parameters
+ (UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size
{
UIImage *img = nil;
// create rectangle/square of size supplied
CGRect rect = CGRectMake(0, 0, size.width, size.height);
// create context of size supplied
UIGraphicsBeginImageContext(rect.size);
// get context reference
CGContextRef context = UIGraphicsGetCurrentContext();
// set fill color
CGContextSetFillColorWithColor(context,color.CGColor);
// fill the color in context
CGContextFillRect(context, rect);
// create image from context
img = UIGraphicsGetImageFromCurrentImageContext();
// end context
UIGraphicsEndImageContext();
// return image
return img;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment