I think I've did same thing before, I have some colored icon, but I needs to keep the shape and change the color to a mono color. So I write an extension to UIImage.
Here is code, hope this help you.
UIImage+MonoImage.h
//
// UIImage+MonoImage.h
// ShiftScheduler
//
// Created by Zhang Jiejing on 12-2-11.
// Copyright (c) 2012. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIImage (MonoImage)
// This function create a Image Context and draw the image with mask
// then clip the context to mask
// then fill the color user choosed.
// that can create a image shape with specify color icon.
+ (UIImage *) generateMonoImage: (UIImage *)icon withColor:(UIColor *)color;
@end
UIImage+MonoImage.m
//
// UIImage+MonoImage.m
// ShiftScheduler
//
// Created by Jiejing Zhang on 12-2-11.
// Copyright (c) 2012. All rights reserved.
//
#import "UIImage+MonoImage.h"
@implementation UIImage (MonoImage)
+ (UIImage *) generateMonoImage: (UIImage *)icon withColor:(UIColor *)color
{
UIImage *finishImage;
CGImageRef alphaImage = CGImageRetain(icon.CGImage);
CGColorRef colorref = CGColorRetain(color.CGColor);
UIGraphicsBeginImageContext(icon.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect imageArea = CGRectMake (0, 0,
icon.size.width, icon.size.height);
// Don't know why if I don't translate the CTM, the image will be a *bottom* up
// aka, head on bottom shape, so I need use TranlateCTM and ScaleCTM to let
// all y-axis to be rotated.
CGFloat height = icon.size.height;
CGContextTranslateCTM(ctx, 0.0, height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextClipToMask(ctx, imageArea , alphaImage);
CGContextSetFillColorWithColor(ctx, colorref);
CGContextFillRect(ctx, imageArea);
CGImageRelease(alphaImage);
CGColorRelease(colorref);
finishImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return finishImage;
}
@end