Skip to content

Instantly share code, notes, and snippets.

@uknowho
Created July 3, 2013 04:02
Show Gist options
  • Save uknowho/5915365 to your computer and use it in GitHub Desktop.
Save uknowho/5915365 to your computer and use it in GitHub Desktop.
A quick way to create an UIImage of a solid color for a specified dimension. You can use this bit of code in an Objective-C category to "extend" UIImage.
+ (UIImage *)imageWithColor:(UIColor *)color andBounds:(CGRect)imgBounds {
UIGraphicsBeginImageContextWithOptions(imgBounds.size, NO, 0);
[color setFill];
UIRectFill(imgBounds);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
@instamobile
Copy link

There's a more elegant solution to this, by loading the image as .alwaysTemplate. Here's a Swift 4 short implementation of how to color an image. Check it out!

@mstana
Copy link

mstana commented Jul 12, 2018

@instamobile, You are proposing totally different thing than the implementation UIImage+withColor.m provides

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