改变图片的颜色,UIImage改变颜色

定义
#import <UIKit/UIKit.h>

@interface UIImage (ChangeImageColor)
/**
 *  改变图片的颜色
 *
 *  @param tintColor <#tintColor description#>
 *
 *  @return <#return value description#>
 */
- (UIImage *) imageWithTintColor:(UIColor *)tintColor;

@end

实现
- (UIImage *)imageWithTintColor:(UIColor *)tintColor{

    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);
    [tintColor setFill];

    CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height);

    UIRectFill(bounds);

    //Draw the tinted image in context
    [self drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:1.0f];

    UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return tintedImage;
}

 

posted @ 2015-12-21 21:09  TheYouth  阅读(724)  评论(0编辑  收藏  举报