Skip to content

Instantly share code, notes, and snippets.

@jimrutherford
Created December 3, 2012 23:49
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jimrutherford/4199145 to your computer and use it in GitHub Desktop.
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
//
// 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
//
// 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
//
// 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
//
// 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