Core Graphics 自定义控件--- 图片类型的上下文

676 阅读3分钟

一、在Core Graphics中有3个概念:CGContext、CALayer、UIView

  1. CGContext是一个画笔,设置绘画的状态参数路径,状态参数有色彩、线宽、粗体等。路径就是path,记录画的图形,有圆、矩形、圆角矩形、不规则图形、线段、弧线等
  2. CALayer是一个画布,ContextRef所画的图形都是在CALayer上
  3. UIView是一个画框,本身并不具备显示的功能,通过CALayer来进行显示,还负责和用户的交互

二、CGContext的3个概念:CGContextRef(图形上下文)、path(路径)、渲染

  1. CGContextRef叫做图形上下文,可以把它简单的认为是一块画布

  2. path就是图形,也叫路径,因为path记录你在CGcontextRef上画的圆形、矩形、线段等各种由线段组组成的图形,说人话就是在脑海中构思,还没有开始画画。

  3. 渲染就是将图形(path)赋予状态参数,说人话就是将构思好的画,画在CGcontextRef上。

     //开启和关闭图形类上下文一定要一一对应
     UIGraphicsBeginImageContext(CGSizeMake(100, 70));//开启图片类型的上下文
     CGcontextRef ctx=UIGraphicsGetCurrentContext();//得到当前的上下文
    
     //创建路径、拼接路径、将路径添加到上下文中、(根据需求去判断剪切还是渲染)
     CGContextMoveToPoint(ctx, 0, 50);
     CGContextAddLineToPoint(ctx, 200, 50);
     CGContextStrokePath(ctx);//描边渲染
     CGContextFillPath(ctx);//填充渲染
     
     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();//通过图片类型的图片上下文 获取图片对象
     UIGraphicsEndImageContext();//关闭图片类型的图形上下文
    

三、样例

UIImageView中可以添加图片,contentMode中也有很多方便的模式。

  1. UIViewContentModeScaleToFill可以填充整个UIImageView,但可能会导致图片变形。
  2. UIViewContentModeScaleAspectFit会保证图片比例不变,但是会有空白。
  3. UIViewContentModeScaleAspectFill只会显示部分图片,且图片比例不变。
  4. UIViewContentModeCenter让整个图片居中,图片比例不变

以上几种模式要么上下填充,要么左右填充,我们可不可以利用Core Graphics方法,去把上下左右都不填充,下面做一个演示
蓝色是UIImageView的范围,现在是原图的效果

image.png

然后是处理过后的效果,把四周设置成不透明

image.png

然后画布染成橘黄色看一下

image.png

- (void)test2 {
   
    UIImage *image = [UIImage imageNamed:@"Image-3"];
    CGFloat x=image.size.width*0.5,y=image.size.height*0.5,width=image.size.width,height=image.size.height,radius=100;
    
    //开启图片类型的上下文
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(image.size.width*2, image.size.height*2), NO, 1);
    
    //给画布染色
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextAddRect(ctx, CGRectMake(0, 0, width*2, height*2));
//    [[UIColor clearColor] setFill];//透明色
    [[UIColor orangeColor] setFill];//橘黄色
    CGContextFillPath(ctx);//渲染
    
    //圆角矩形裁剪
    CGContextAddRoundRect(ctx, CGRectMake(x, y, width, height), 0.1*MAX(width, height));
    CGContextClip(ctx);//裁剪
    
    //把图片画上去
    [image drawAtPoint:CGPointMake(x, y)];
    //取出来
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //关闭图片类型的图形上下文
    UIGraphicsEndImageContext();
    //测试
    self.imageView.image=newImage;
}

void CGContextAddRoundRect(CGContextRef context,CGRect rect,CGFloat radius){
    float x1=rect.origin.x;
    float y1=rect.origin.y;
    float x2=x1+rect.size.width;
    float y2=y1;
    float x3=x2;
    float y3=y1+rect.size.height;
    float x4=x1;
    float y4=y3;
    CGContextMoveToPoint(context, x1, y1+radius);
    CGContextAddArcToPoint(context, x1, y1, x1+radius, y1, radius);
    CGContextAddArcToPoint(context, x2, y2, x2, y2+radius, radius);
    CGContextAddArcToPoint(context, x3, y3, x3-radius, y3, radius);
    CGContextAddArcToPoint(context, x4, y4, x4, y4-radius, radius);
    CGContextClosePath(context);
}