CGContext

CGContext 图形上下文 通俗讲就是一块画板,栈形式存放。iOS又分多种图形上下文,其中UIView自带提供的在drawRect方法中通过 UIGraphicsGetCurrentContext获取,还有专门为图片处理的context,还有pdf的context等等均有特定的获取方法

常用方法

 CGContext常用方法
 //获取当前上下文
 CGContextRef context = UIGraphicsGetCurrentContext();
 
 //开始一起起点路径
 CGContextBeginPath(context);
 
 //移动画笔
 CGContextMoveToPoint(context,100, 100);
 
 //直线(在draw时才时整帧平装绘制)
 CGContextAddLineToPoint(context,200, 200);
 //绘制椭圆
 // CGContextAddEllipseInRect(<#CGContextRef  _Nullable c#>, <#CGRect rect#>)
 //CGContextFillEllipseInRect(<#CGContextRef  _Nullable c#>, <#CGRect rect#>)
 
 
 
 //画虚线
 //CGContextSetLineDash(<#CGContextRef  _Nullable c#>, <#CGFloat phase#>, <#const CGFloat * _Nullable lengths#>, <#size_t count#>)
 
 
 
 //画矩形
 // CGContextAddRect(<#CGContextRef  _Nullable c#>, <#CGRect rect#>)
 //指定矩形
 // CGContextStrokeRect(<#CGContextRef  _Nullable c#>, <#CGRect rect#>)
 //指定矩形线宽度
 // CGContextStrokeRectWithWidth(<#CGContextRef  _Nullable c#>, <#CGRect rect#>, <#CGFloat width#>)
 
 //画曲线
 方式1
 CGContextMoveToPoint(context,100,100);
 
 CGContextAddArcToPoint(context,50,100,50,150,50);
 //方式二 前两点为中心,中间两点为起始护短,最后一数据为0顺时针嗯,1逆时针
 CGContextAddArc(<#CGContextRef  _Nullable c#>, <#CGFloat x#>, <#CGFloat y#>, <#CGFloat radius#>, <#CGFloat startAngle#>, <#CGFloat endAngle#>, <#int clockwise#>)
 
 //方式三
 // CGContextAddQuadCurveToPoint(<#CGContextRef  _Nullable c#>, <#CGFloat cpx#>, <#CGFloat cpy#>, <#CGFloat x#>, <#CGFloat y#>)

常用属性

//设置线装末端形状
 /枚举
 kCGLineCapRound 圆
 kCGLineCapButt
 kCGLineCapSquare
 /
//CGContextSetLineCap(context, kCGLineCapSquare);


//设置线条粗细
//CGContextSetLineWidth(<#CGContextRef  _Nullable c#>, <#CGFloat width#>)

//设置线条颜色
// CGContextSetRGBStrokeColor(<#CGContextRef  _Nullable c#>, <#CGFloat red#>, <#CGFloat green#>, <#CGFloat blue#>, <#CGFloat alpha#>)

//颜色控件画笔设置
//CGContextSetStrokeColorSpace(<#CGContextRef  _Nullable c#>, <#CGColorSpaceRef  _Nullable space#>)


//设置填充颜色填充颜色 需要和下面这个方法使用
//CGContextSetRGBFillColor(<#CGContextRef  _Nullable c#>, <#CGFloat red#>, <#CGFloat green#>, <#CGFloat blue#>, <#CGFloat alpha#>)
 //填充色形状
 //CGContextFillRect(context, CGRectMake(0, 0,200,200));
 
 
 
 
//颜色控件填充
//CGContextSetFillColorSpace(<#CGContextRef  _Nullable c#>, <#CGColorSpaceRef  _Nullable space#>)



//画一些线段
//CGContextStrokeLineSegments(<#CGContextRef  _Nullable c#>, <#const CGPoint * _Nullable points#>, <#size_t count#>)




//先画两条线,从point到第一点,从第一点到底2点,切割里面的圆
//CGContextAddArcToPoint

//设置阴影
//  CGContextSetShadowWithColor(<#CGContextRef  _Nullable c#>, <#CGSize offset#>, <#CGFloat blur#>, <#CGColorRef  _Nullable color#>)

//透明度
//CGContextSetAlpha(<#CGContextRef  _Nullable c#>, <#CGFloat alpha#>)



//开始绘制
// CGContextStrokePath(<#CGContextRef  _Nullable c#>)

//闭合
// CGContextClosePath(<#CGContextRef  _Nullable c#>)

//设置绘制模式
//  CGContextDrawPath(<#CGContextRef  _Nullable c#>, <#CGPathDrawingMode mode#>)

demo

  CGContextRef context = UIGraphicsGetCurrentContext();
    
    
    //画矩形
    // CGContextAddRect(context, CGRectMake(0, 0,100,100));
    //设置线条颜色
    CGContextSetRGBStrokeColor(context, 0.21, 0.71, 0.95,1);
    //设置填充色颜色
    //CGContextSetRGBFillColor(context, 0.94, 0.37, 0.21,1);
    //画笔路径宽度
    CGContextSetLineWidth(context,10);
     //填充颜色
    //CGContextFillPath(context);
    //开始绘制
   
    
    
    
    //画直线
    
//    CGContextMoveToPoint(context,120,0);
//    CGContextAddLineToPoint(context,120, 150);
//    CGContextAddLineToPoint(context,200, 200);
   // CGContextStrokePath(context);
    
    
    //椭圆
    CGContextAddEllipseInRect(context,CGRectMake(100,0,100,70));
    
    //弧线
//    CGContextMoveToPoint(context,100,100);
//
//    CGContextAddArcToPoint(context,50,100,50,150,50);
//
//    CGContextAddArc(context,100,100,50,180* M_PI/ 180,270* M_PI/ 180,0);
    
    //用法相同
    // CGContextStrokePath(context);
    CGContextDrawPath(context,kCGPathStroke);

demo2
字符串绘制

  //字符串绘制
    NSString* text = @"This is English text(NSString).";
     [text drawAtPoint:CGPointMake(0, 0) withFont:[UIFont systemFontOfSize:20]];
    
    text = @"这是中文文本(NSString)。";
    [text drawAtPoint:CGPointMake(0, 50) withFont:[UIFont systemFontOfSize:20]];
    
    CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0, -1.0));
    CGContextSelectFont(context, "Helvetica", 20, kCGEncodingMacRoman);
    const char* str = "This is English text(Quartz 2D).";
    
    CGContextShowTextAtPoint(context, 0, 100, str, strlen(str));
    const char* str1 = "这是中文文本(Quartz 2D)。";
    CGContextShowTextAtPoint(context, 0, 150, str1, strlen(str1));

demo3
截取图片中的某部分

  //截取图片中的的小图
    UIImage *imagev = [UIImage imageNamed:@"timg.jpg"];
   CGImageRef newImageRef = CGImageCreateWithImageInRect(imagev.CGImage, CGRectMake(0, 0,50, 50));
    
    UIImageView *i  = [[UIImageView alloc]initWithImage:[UIImage imageWithCGImage:newImageRef]];
    i.frame = CGRectMake(100,100,100, 100);
    [self addSubview:i];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值