iOS利用代码修改图片颜色实用get√技能

前一段时间接到一个需求,说我们的应用需要换一整套皮肤,图片的形状大小尺寸甚至命名都不用修改,无疑这个时候UI设计师和我们的工作显得有些重复而笨重。

无意间找到了一个用代码修改图片颜色,代码无痛接入,简直酸爽!

@interface UIImage (TintCorlor)
///修改图片颜色
- (UIImage *) imageWithTintColor:(UIColor *)tintColor;
///修改图片颜色 同时保留透明度
- (UIImage *) imageWithGradientTintColor:(UIColor *)tintColor;

@end


- (UIImage *) imageWithTintColor:(UIColor *)tintColor{
    return [self imageWithTintColor:tintColor blendMode:kCGBlendModeDestinationIn];
}

- (UIImage *) imageWithGradientTintColor:(UIColor *)tintColor{
    return [self imageWithTintColor:tintColor blendMode:kCGBlendModeOverlay];
}

- (UIImage *) imageWithTintColor:(UIColor *)tintColor blendMode:(CGBlendMode)blendMode{
        //We want to keep alpha, set opaque to NO; Use 0.0f for scale to use the scale factor of the device’s main screen.
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);
    [tintColor setFill];
    CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height);
    UIRectFill(bounds);
        //Draw the tinted image in context
    [self drawInRect:bounds blendMode:blendMode alpha:1.0f];
    if (blendMode != kCGBlendModeDestinationIn) {
        [self drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:1.0f];
    }
    UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return tintedImage;
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,976评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,198评论 4 61
  • 瑟瑟的秋风席卷着落叶,落雨把温度一降再降。秋天,是一个容易感伤的季节,是一个容易想起一些过往然后一整天都深深沉浸的...
    巫婆爱哭阅读 452评论 0 2
  • 目录 上一章 到了约定时间,韩一飞带着张丽娜和东方慧来到会议室,令三人没有想到的是,会议室里不仅金总经理和他的助理...
    小静读童书阅读 526评论 0 2
  • 1. round(x,d) 把x保留d位小数 2. period_diff() 相隔月/年description ...
    zzjack阅读 281评论 0 0