美文网首页
修改图片颜色,全局控制色调

修改图片颜色,全局控制色调

作者: 进击的小杰 | 来源:发表于2017-08-17 14:28 被阅读36次
    #import <UIKit/UIKit.h>
    
    @interface UIImage (changeImage)
    
    - (UIImage *) imageWithTintColor:(UIColor *)tintColor;
    
    - (UIImage *) imageWithGradientTintColor:(UIColor *)tintColor;
    
    @end
    
    _________________________________
    _________________________________
    
    #import "UIImage+changeImage.h"
    
    @implementation UIImage (changeImage)
    
    - (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;
        
    }
    
    @end
    
    调用
            UIImage *image=[UIImage imageNamed:@"today_red"];
            UIImage *changeImage=[image imageWithTintColor:[mnResource colorWithHexString:NavColor]];
            self.adView.headImg = changeImage;
    

    相关文章

      网友评论

          本文标题:修改图片颜色,全局控制色调

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