Created
December 3, 2012 23:49
-
-
Save jimrutherford/4199145 to your computer and use it in GitHub Desktop.
A simple category on UIImage that will tint a named image with a UIColor
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+ImageWithColor.h | |
// WordClock | |
// | |
// Created by James Rutherford on 2012-12-03. | |
// Copyright (c) 2012 Braxio Interactive. All rights reserved. | |
// | |
@interface UIImage (ImageWithColor) | |
+ (UIImage *)imageNamed:(NSString *)name imageWithColor:(UIColor *)color; | |
@end |
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+ImageWithColor.m | |
// WordClock | |
// | |
// Created by James Rutherford on 2012-12-03. | |
// Copyright (c) 2012 Braxio Interactive. All rights reserved. | |
// | |
#import "UIImage+ImageWithColor.h" | |
@implementation UIImage (ImageWithColor) | |
+ (UIImage *)imageNamed:(NSString *)name imageWithColor:(UIColor *)color | |
{ | |
UIImage *img = nil; | |
img = [UIImage imageNamed:name]; | |
// lets tint the icon - assumes your icons are black | |
UIGraphicsBeginImageContext(img.size); | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
CGContextTranslateCTM(context, 0, img.size.height); | |
CGContextScaleCTM(context, 1.0, -1.0); | |
CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height); | |
// draw alpha-mask | |
CGContextSetBlendMode(context, kCGBlendModeNormal); | |
CGContextDrawImage(context, rect, img.CGImage); | |
// draw tint color, preserving alpha values of original image | |
CGContextSetBlendMode(context, kCGBlendModeSourceIn); | |
[color setFill]; | |
CGContextFillRect(context, rect); | |
UIImage *coloredImage = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return coloredImage; | |
} | |
@end |
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+ImageWithColor.h | |
// WordClock | |
// | |
// Created by James Rutherford on 2012-12-03. | |
// Copyright (c) 2012 Braxio Interactive. All rights reserved. | |
// | |
@interface UIImage (ImageWithColor) | |
+ (UIImage *)imageNamed:(NSString *)name imageWithColor:(UIColor *)color; | |
@end |
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+ImageWithColor.m | |
// WordClock | |
// | |
// Created by James Rutherford on 2012-12-03. | |
// Copyright (c) 2012 Braxio Interactive. All rights reserved. | |
// | |
#import "UIImage+ImageWithColor.h" | |
@implementation UIImage (ImageWithColor) | |
+ (UIImage *)imageNamed:(NSString *)name imageWithColor:(UIColor *)color | |
{ | |
UIImage *img = nil; | |
img = [UIImage imageNamed:name]; | |
// lets tint the icon - assumes your icons are black | |
UIGraphicsBeginImageContext(img.size); | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
CGContextTranslateCTM(context, 0, img.size.height); | |
CGContextScaleCTM(context, 1.0, -1.0); | |
CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height); | |
// draw alpha-mask | |
CGContextSetBlendMode(context, kCGBlendModeNormal); | |
CGContextDrawImage(context, rect, img.CGImage); | |
// draw tint color, preserving alpha values of original image | |
CGContextSetBlendMode(context, kCGBlendModeSourceIn); | |
[color setFill]; | |
CGContextFillRect(context, rect); | |
UIImage *coloredImage = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
return coloredImage; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment