美文网首页
2019-03-22

2019-03-22

作者: Angel_梅丹佐 | 来源:发表于2019-03-22 15:50 被阅读0次

    iOS 开发之修改图片image颜色

    吐槽:平时开发中可能因为 UI妹子懒给到图片中没有需要的颜色,或者嫌弃UI 妹子给图片太慢,(ps:目前公司 UI tm 一男的)所以就有了程序员“高大上”的一面:代码修改图片颜色。好了,不吐槽了,下面上代码:

    /*
        类方法(可改为对象方法)
        返回:UIImage *
        color: 需要改成的颜色
        image:需要修改的 image
        alpha:设置颜色的透明度
    */
    //改变图片颜色
    + (UIImage *)changeImageColorWith:(UIColor *)color image:(UIImage *)image alpha:(CGFloat)alpha
    {
        UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextTranslateCTM(context, 0, image.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
        CGContextSetAlpha(context, alpha);
        CGContextSetBlendMode(context, kCGBlendModeNormal);
        CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
        CGContextClipToMask(context, rect, image.CGImage);
        [color setFill];
        CGContextFillRect(context, rect);
        UIImage*newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage;
    }
    

    相关文章

      网友评论

          本文标题:2019-03-22

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