iOS经验总结

iOS UIImage自定义颜色

2017-01-18  本文已影响260人  Realank
#import "UIImage+ChangeColor.h"

@implementation UIImage (ChangeColor)

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

@end
上一篇下一篇

猜你喜欢

热点阅读