给UIImage 画背景颜色and 给图片画颜色

最近接到需求,项目的图片要随着主题色的改变,做相应的改变,在这里取两种类型的图片来做说明

第一种图片是:背景色跟随主题,图片内容为白色,思路是先画个纯色主题色背景图片,然后把白色的图片和你所画的背景图片做拼接,实现如下:

Snip20160113_3.png
UIImage+Extension.h文件
#import 
@interface UIImage (Extension)
/** 染背景色*/
- (UIImage *)image:(UIImage *)image withColor:(UIColor *)color;
/** 染内容*/
- (UIImage *)imageContentWithColor:(UIColor *)color ;
/** 拼接图片*/
- (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2;

UIImage+Extension.m文件

// 画背景
- (UIImage *)image:(UIImage *)image withColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);    
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, image.scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [image drawInRect:rect];
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextSetBlendMode(context, kCGBlendModeNormal);
    CGContextFillRect(context, rect);
    
    UIImage*newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

// 合成图片
- (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2 {
    UIGraphicsBeginImageContext(image1.size);
    
    // Draw image2  哪个图片在最下面先画谁,在这里有先后顺序
    [image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];
    
    // Draw image1
    [image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];
    
    
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return resultingImage;
}

// 画带有边框的圆,在画这个图片的时候用到了,也写出来吧,但是边框设的为零,
- (UIImage *)circleImageName:(NSString *)path borderWith:(double)border colorWith:(UIColor *)color
{
    UIImage *img=[UIImage imageNamed:path];
    UIGraphicsBeginImageContext(img.size );
    
    CGContextRef ctr =UIGraphicsGetCurrentContext();
   
    double radius=img.size.height>img.size.width?(img.size.width/2):(img.size.height/2);
    
    radius/=2;
    
    double centerx=img.size.width/2;
    double centery=img.size.height/2;

    [color set];
    //   CGContextSetLineWidth(ctr, border);
    CGContextAddArc(ctr, centerx, centery, radius+border,0, M_PI_2*4,YES);
    CGContextFillPath(ctr);
    
    CGContextAddArc(ctr, centerx, centery, radius,0, M_PI_2*4,YES);
    CGContextClip(ctr);

    [img drawInRect:CGRectMake(0,0, img.size.width, img.size.height)];
  
    UIImage *newImg=UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    return newImg;

}

第二种图片就是:图片内容随着主题色改变其他背景透明


Snip20160113_2.png

UIImage+Extension.m文件

- (UIImage *)imageContentWithColor:(UIColor *)color {
    if (!color) {
        return nil;
    }
    
    UIImage *newImage = nil;
    
    CGRect imageRect = (CGRect){CGPointZero,self.size};
    UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, self.scale);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextTranslateCTM(context, 0.0, -(imageRect.size.height));
    
    CGContextClipToMask(context, imageRect, self.CGImage);//选中选区 获取不透明区域路径
    CGContextSetFillColorWithColor(context, color.CGColor);//设置颜色
    CGContextFillRect(context, imageRect);//绘制
    
    newImage = UIGraphicsGetImageFromCurrentImageContext();//提取图片
    
    UIGraphicsEndImageContext();
    return newImage;
}

第二种图片借鉴网友提供的思路,特此感谢!

你可能感兴趣的:(给UIImage 画背景颜色and 给图片画颜色)