iOS 修改图片颜色

给UIImage 添加一个分类,并给分类添加一个方法, 该方法的实现如下

- (UIImage *)tp_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;
}

猜你喜欢

转载自blog.csdn.net/LIUXIAOXIAOBO/article/details/131405301