改变图片颜色
2016-11-17 本文已影响0人
京哥
//改变图片颜色
- (UIImage *)imageWithColor:(UIColor *)color image:(NSString *)image
{
UIGraphicsBeginImageContextWithOptions(self.frame.size, NO,2);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, self.frame.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGRect rect = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
//转换 UIImage 的格式
UIImage *loadImage=[UIImage imageNamed:image];
CGImageRef cgimage=loadImage.CGImage;
CGContextClipToMask(context, rect,cgimage);
[color setFill];
CGContextFillRect(context, rect);
UIImage*newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}