Skip to content

Instantly share code, notes, and snippets.

@kevinoneill
Created March 21, 2011 03:32
Show Gist options
  • Save kevinoneill/878969 to your computer and use it in GitHub Desktop.
Save kevinoneill/878969 to your computer and use it in GitHub Desktop.
Tinting UIImages
//
// UIImage+Size.h
// Quickie
//
// Created by Kevin O'Neill on 13/03/11.
// Copyright 2011 Kevin O'Neill. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIImage (Size)
- (CGFloat)width;
- (CGFloat)height;
- (CGRect)bounds;
@end
//
// UIImage+Size.m
// Quickie
//
// Created by Kevin O'Neill on 13/03/11.
// Copyright 2011 Kevin O'Neill. All rights reserved.
//
#import "UIImage+Size.h"
@implementation UIImage (Size)
- (CGFloat)width;
{
return [self size].width;
}
- (CGFloat)height;
{
return [self size].height;
}
- (CGRect)bounds;
{
CGSize size = [self size];
return CGRectMake(0., 0., size.width, size.height);
}
@end
// Quickie
//
// Created by Kevin O'Neill on 15/03/11.
// Copyright 2011 Kevin O'Neill. All rights reserved.
//
#import <UIKit/UIImage.h>
@interface UIImage (Tint)
+ (UIImage *)imageNamed:(NSString *)name tint:(UIColor *)tint;
- (UIImage *)tint:(UIColor *)tint;
@end
//
// Tint.m
// Quickie
//
// Created by Kevin O'Neill on 15/03/11.
// Copyright 2011 Kevin O'Neill. All rights reserved.
//
#import "UIImage+Tint.h"
#import "UIImage+Size.h"
@interface TintedImageCache : NSObject
{
@private
NSMutableDictionary *cache_;
}
- (UIImage *)imageNamed:(NSString *)name tint:(UIColor *)tint;
- (void)addImageNamed:(NSString *)name tint:(UIColor *)tint image:(UIImage *)image;
@end
@implementation UIImage (Tint)
+ (TintedImageCache *)tintCache;
{
static dispatch_once_t cache_initialized;
static TintedImageCache *cache;
dispatch_once(&cache_initialized, ^{
cache = [[TintedImageCache alloc] init];
});
return cache;
}
+ (UIImage *)imageNamed:(NSString *)name tint:(UIColor *)tint;
{
UIImage *result = [[self tintCache] imageNamed:name tint:tint];
if (nil == result)
{
result = [[self imageNamed:name] tint:tint];
if (nil != result)
{
[[self tintCache] addImageNamed:name tint:tint image:result];
}
}
return result;
}
- (UIImage *)tint:(UIColor *)tint;
{
NSAssert(tint != nil, @"tint must not be nil");
UIGraphicsBeginImageContextWithOptions([self size], NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, [self height]);
CGContextScaleCTM(context, 1.0, -1.0);
[tint setFill];
CGRect bounds = [self bounds];
CGContextSetBlendMode(context, kCGBlendModeCopy);
CGContextDrawImage(context, bounds, [self CGImage]);
CGContextSetBlendMode(context, kCGBlendModeSourceIn);
CGContextFillRect(context, bounds);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end
inline static NSString *cachekey(NSString *name, UIColor *tint)
{
return [name stringByAppendingPathComponent:[tint description]];
}
@implementation TintedImageCache
// TODO: KAO - Add listener to memory warnings.
- (id)init
{
if ((self = [super init]))
{
cache_ = [NSMutableDictionary dictionaryWithCapacity:13];
}
return self;
}
- (void)dealloc
{
[cache_ release];
[super dealloc];
}
- (UIImage *)imageNamed:(NSString *)name tint:(UIColor *)tint;
{
return [cache_ objectForKey:cachekey(name, tint)];
}
- (void)addImageNamed:(NSString *)name tint:(UIColor *)tint image:(UIImage *)image;
{
[cache_ setObject:image forKey:cachekey(name, tint)];
}
@end
@kevinoneill
Copy link
Author

kevinoneill commented Mar 24, 2011 via email

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