Created
May 13, 2013 08:18
-
-
Save agiletalk/5566881 to your computer and use it in GitHub Desktop.
How to change the color of an UIImage
This file contains hidden or 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
+ (UIImage *)imageNamed:(NSString *)name withColor:(UIColor *)color; | |
{ | |
UIImage *image = [UIImage imageNamed:name]; | |
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height); | |
UIGraphicsBeginImageContext(rect.size); | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
CGContextClipToMask(context, rect, image.CGImage); | |
CGContextSetFillColorWithColor(context, color.CGColor); | |
CGContextFillRect(context, rect); | |
UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
UIImage *coloredImage = [UIImage imageWithCGImage:img.CGImage scale:1.0 orientation:UIImageOrientationDownMirrored]; | |
return coloredImage; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Many thanks!