Created
July 24, 2011 01:40
-
-
Save omz/1102091 to your computer and use it in GitHub Desktop.
Creating arbitrarily-colored icons from a black-with-alpha master image (iOS)
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
// Usage example: | |
// input image: http://f.cl.ly/items/3v0S3w2B3N0p3e0I082d/Image%202011.07.22%2011:29:25%20PM.png | |
// | |
// UIImage *buttonImage = [UIImage ipMaskedImageNamed:@"UIButtonBarAction.png" color:[UIColor redColor]]; | |
// .h | |
@interface UIImage (IPImageUtils) | |
+ (UIImage *)ipMaskedImageNamed:(NSString *)name color:(UIColor *)color; | |
@end | |
// .m | |
@implementation UIImage (IPImageUtils) | |
+ (UIImage *)ipMaskedImageNamed:(NSString *)name color:(UIColor *)color | |
{ | |
UIImage *image = [UIImage imageNamed:name]; | |
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height); | |
UIGraphicsBeginImageContextWithOptions(rect.size, NO, image.scale); | |
CGContextRef c = UIGraphicsGetCurrentContext(); | |
[image drawInRect:rect]; | |
CGContextSetFillColorWithColor(c, [color CGColor]); | |
CGContextSetBlendMode(c, kCGBlendModeSourceAtop); | |
CGContextFillRect(c, rect); | |
UIImage *result = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return result; | |
} | |
@end |
Great gist. Has anyone updated/forked the above based on the last 2 comments?
If you have a UIImage instance instead of the name...
-(UIImage * _Nullable)tintMaskWithColor:(UIColor *_Nonnull)color{
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
CGContextRef c = UIGraphicsGetCurrentContext();
[self drawInRect:rect];
CGContextSetFillColorWithColor(c, [color CGColor]);
CGContextSetBlendMode(c, kCGBlendModeSourceAtop);
CGContextFillRect(c, rect);
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
faithfracture is very wise. Leaving off the UIGraphicsEndImageContext() will cause a memory leak in your application. I know, because it happened to us after a developer used this code.