【深入浅出IOS开发】图形上下文栈

来源:互联网 发布:哔哩哔哩没有mac版吗 编辑:程序博客网 时间:2024/05/01 18:45

图形上下文栈通过一种栈的形式保存的绘制格式。可以方便我们在设置过绘制格式之后,进行恢复。

 ①开始绘制之前,保存设备上下文

    CGContextSaveGState(ctr);

 ②渲染之后,恢复设备上下文

    CGContextRestoreGState(ctr);

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //图形上下文栈  
  2. void drawLine(CGRect rect, CGContextRef ctr)  
  3. {  
  4.     //开始绘制之前,保存设备上下文  
  5.     CGContextSaveGState(ctr);  
  6.       
  7.     CGContextMoveToPoint(ctr, 1010);  
  8.     CGContextAddLineToPoint(ctr, 340500);  
  9.     [[UIColor redColor]set];  
  10.     CGContextSetLineWidth(ctr, 20);  
  11.     CGContextSetLineCap(ctr,kCGLineCapRound);  
  12.     CGContextStrokePath(ctr);  
  13.     //渲染之后,恢复设备上下文  
  14.     CGContextRestoreGState(ctr);  
  15.       
  16.     CGContextMoveToPoint(ctr, 350100);  
  17.     CGContextAddLineToPoint(ctr, 0300);  
  18.     CGContextStrokePath(ctr);  
  19. }  

0 0
原创粉丝点击