Created
March 21, 2011 03:32
-
-
Save kevinoneill/878969 to your computer and use it in GitHub Desktop.
Tinting UIImages
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
// | |
// 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 |
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
// | |
// 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 |
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
// 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 |
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
// | |
// 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 |
Yeah , whoops
…On 24/03/2011, at 1:42 PM, ittybittydude ***@***.*** wrote:
Hey Kev, you might want to try retaining cache_
;-)
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/878969
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey Kev, you might want to try retaining cache_
;-)