【iOS开发-80】Quartz2D绘图简介:直线/圆形/椭圆/方形以及上下文栈管理CGContextSaveGState/CGContextRestoreGState

本文转载至 http://blog.csdn.net/weisubao/article/details/41282457 

  1. - (void)drawRect:(CGRect)rect {  
  2.     //获得当前上下文  
  3.     CGContextRef ctx=UIGraphicsGetCurrentContext();  
  4.       
  5.     //把当前上下文状态保存在栈中  
  6.     CGContextSaveGState(ctx);  
  7.       
  8.     //缩放、移动处理(需要放在画图之前进行设置)  
  9.     CGContextScaleCTM(ctx, 0.5, 0.5);  
  10.     CGContextTranslateCTM(ctx, 100, 100);  
  11.     CGContextRotateCTM(ctx, M_PI_4);  
  12.       
  13.     //描点  
  14.     CGContextMoveToPoint(ctx, 10, 10);  
  15.     CGContextAddLineToPoint(ctx, 100, 100);  
  16.     CGContextAddLineToPoint(ctx, 150, 50);  
  17.     //以下两种方式均可闭环  
  18.     //CGContextAddLineToPoint(ctx, 10, 10);  
  19.     CGContextClosePath(ctx);  
  20.     //渲染绘图,实心和空心  
  21.     CGContextStrokePath(ctx);  
  22.     //CGContextFillPath(ctx);  
  23.       
  24.     //把当前上下文状态保存在栈中  
  25.     CGContextSaveGState(ctx);  
  26.       
  27.     //画正方形  
  28.     CGContextAddRect(ctx, CGRectMake(100, 100, 50, 50));  
  29.     //设置线宽(一定要在CGContextStrokePath之前)  
  30.     //因为之前有过一次渲染绘图,所以这个属性设置不影响上面的那个三角形,以下颜色设置同理  
  31.     //所以,如果想分别设置两个或多个图形的属性,就分别渲染绘图一次  
  32.     CGContextSetLineWidth(ctx, 10);  
  33.     //设置颜色(同理,属性设置的代码都要在绘图的代码之前)  
  34.     CGContextSetRGBStrokeColor(ctx, 1, 0, 0, 1);  
  35.     CGContextStrokePath(ctx);  
  36.       
  37.     //设置样式  
  38.     CGContextMoveToPoint(ctx, 20, 160);  
  39.     CGContextAddLineToPoint(ctx, 200, 280);  
  40.     CGContextAddLineToPoint(ctx, 250, 200);  
  41.     CGContextSetLineWidth(ctx, 20);  
  42.     //设置头尾样式  
  43.     CGContextSetLineCap(ctx, kCGLineCapRound);  
  44.     //设置转角样式  
  45.     CGContextSetLineJoin(ctx, kCGLineJoinRound);  
  46.     CGContextStrokePath(ctx);  
  47.       
  48.     //把保存在栈中的上下文状态取出来,恢复。上面那段代码设置的样式不会影响其他  
  49.     CGContextRestoreGState(ctx);  
  50.       
  51.     //画椭圆  
  52.     CGContextAddEllipseInRect(ctx, CGRectMake(200, 130, 60, 30));  
  53.     //以下等价  
  54.     //CGContextStrokePath(ctx);  
  55.     CGContextDrawPath(ctx, kCGPathStroke);  
  56.       
  57.     //画圆形  
  58.     CGContextAddEllipseInRect(ctx, CGRectMake(140, 170, 50, 50));  
  59.     CGContextSetLineWidth(ctx, 3);  
  60.     CGContextStrokePath(ctx);  
  61.       
  62.     //画圆弧  
  63.     CGContextAddArc(ctx, 200, 50, 50, M_PI_4, M_PI, 1);  
  64.     CGContextStrokePath(ctx);  
  65.       
  66.     //画1/4圆,以及颜色的设置新方法  
  67.     CGContextMoveToPoint(ctx, 10, 230);  
  68.     CGContextAddLineToPoint(ctx, 10, 280);  
  69.     CGContextAddLineToPoint(ctx, 60, 280);  
  70.     CGContextAddArc(ctx, 10, 280, 50, 0, -M_PI_2, 1);  
  71.     [[UIColor greenColor] setStroke];  
  72.     CGContextStrokePath(ctx);  
  73.       
  74.     //画图片和文字(不需要手动取得上下文)  
  75.     NSString *str1=@"辛丑年一空作";  
  76.     [str1 drawAtPoint:CGPointZero withAttributes:nil];  
  77.     UIImage *img=[UIImage imageNamed:@"001"];  
  78.     [img drawAtPoint:CGPointMake(10, 10)];  
  79.     //在一个框框里重叠图片并署名  
  80.     CGRect rect1=CGRectMake(50, 50, 100, 100);  
  81.     [img drawAsPatternInRect:rect1];  
  82.     NSMutableDictionary *attr=[[NSMutableDictionary alloc]init];  
  83.     attr[NSForegroundColorAttributeName]=[UIColor whiteColor];  
  84.     attr[NSFontAttributeName]=[UIFont systemFontOfSize:13];  
  85.     [str1 drawInRect:CGRectMake(50, 140, 100, 100) withAttributes:attr];  
  86.       
  87.     //把保存在栈中的上下文状态取出来,恢复。上面那段代码设置的样式不会影响其他  
  88.     CGContextRestoreGState(ctx);  
  89.       
  90.     //裁剪圆形头像  
  91.     CGContextAddEllipseInRect(ctx, CGRectMake(150, 150, 100 , 100));  
  92.     //按照圆形剪裁出一个上下文区域,以后的内容就填充在这个圆形上下文中  
  93.     CGContextClip(ctx);  
  94.     UIImage *img1=[UIImage imageNamed:@"me"];  
  95.     [img1 drawAtPoint:CGPointMake(150, 150)];  
  96. }  
原文地址:https://www.cnblogs.com/Camier-myNiuer/p/4180145.html