美文网首页iOS经验总结
iOS UIImage自定义颜色

iOS UIImage自定义颜色

作者: Realank | 来源:发表于2017-01-18 16:32 被阅读260次
    #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
    

    相关文章

      网友评论

        本文标题:iOS UIImage自定义颜色

        本文链接:https://www.haomeiwen.com/subject/pvoubttx.html