星五博客

改变图片颜色(转贴+修改)

原贴:http://www.cocoachina.com/bbs/read.php?tid=181448

#pragma mark 改变图片颜色
- (UIImage *)imageWithColor:(UIColor *)color :(UIImage *)sourceImage
{
    UIGraphicsBeginImageContextWithOptions(sourceImage.size, NO, sourceImage.scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0, sourceImage.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextSetBlendMode(context, kCGBlendModeNormal);
    CGRect rect = CGRectMake(0, 0, sourceImage.size.width, sourceImage.size.height);
    CGContextClipToMask(context, rect, sourceImage.CGImage);
    [color setFill];
    CGContextFillRect(context, rect);
    UIImage*newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

调用:

userNameImage.image=[selfimageWithColor:ColorHex(ColorMain) :userNameImage.image];


IOS