Skip to content

Instantly share code, notes, and snippets.

@liuzhida33
Last active July 12, 2018 05:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liuzhida33/d3d3543f1e745c51ebd45a9b55e54651 to your computer and use it in GitHub Desktop.
Save liuzhida33/d3d3543f1e745c51ebd45a9b55e54651 to your computer and use it in GitHub Desktop.
iOS修改图片颜色
- (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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment