Quartz2D绘图简介:直线/圆形/椭圆/方形以及上下文栈管理CGContextSaveGState/CGContextRestoreGState




  1. - (void)drawRect:(CGRect)rect {  
  2.     //获得当前上下文  
  3.     CGContextRef ctx=UIGraphicsGetCurrentContext();  
  4.       
  5.     //把当前上下文状态保存在栈中  
  6.     CGContextSaveGState(ctx);  
  7.       
  8.     //缩放、移动处理(需要放在画图之前进行设置)  
  9.     CGContextScaleCTM(ctx, 0.50.5);  
  10.     CGContextTranslateCTM(ctx, 100100);  
  11.     CGContextRotateCTM(ctx, M_PI_4);  
  12.       
  13.     //描点  
  14.     CGContextMoveToPoint(ctx, 1010);  
  15.     CGContextAddLineToPoint(ctx, 100100);  
  16.     CGContextAddLineToPoint(ctx, 15050);  
  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(1001005050));  
  29.     //设置线宽(一定要在CGContextStrokePath之前)  
  30.     //因为之前有过一次渲染绘图,所以这个属性设置不影响上面的那个三角形,以下颜色设置同理  
  31.     //所以,如果想分别设置两个或多个图形的属性,就分别渲染绘图一次  
  32.     CGContextSetLineWidth(ctx, 10);  
  33.     //设置颜色(同理,属性设置的代码都要在绘图的代码之前)  
  34.     CGContextSetRGBStrokeColor(ctx, 1001);  
  35.     CGContextStrokePath(ctx);  
  36.       
  37.     //设置样式  
  38.     CGContextMoveToPoint(ctx, 20160);  
  39.     CGContextAddLineToPoint(ctx, 200280);  
  40.     CGContextAddLineToPoint(ctx, 250200);  
  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(2001306030));  
  53.     //以下等价  
  54.     //CGContextStrokePath(ctx);  
  55.     CGContextDrawPath(ctx, kCGPathStroke);  
  56.       
  57.     //画圆形  
  58.     CGContextAddEllipseInRect(ctx, CGRectMake(1401705050));  
  59.     CGContextSetLineWidth(ctx, 3);  
  60.     CGContextStrokePath(ctx);  
  61.       
  62.     //画圆弧  
  63.     CGContextAddArc(ctx, 2005050, M_PI_4, M_PI, 1);  
  64.     CGContextStrokePath(ctx);  
  65.       
  66.     //画1/4圆,以及颜色的设置新方法  
  67.     CGContextMoveToPoint(ctx, 10230);  
  68.     CGContextAddLineToPoint(ctx, 10280);  
  69.     CGContextAddLineToPoint(ctx, 60280);  
  70.     CGContextAddArc(ctx, 10280500, -M_PI_21);  
  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(1010)];  
  79.     //在一个框框里重叠图片并署名  
  80.     CGRect rect1=CGRectMake(5050100100);  
  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(50140100100) withAttributes:attr];  
  86.       
  87.     //把保存在栈中的上下文状态取出来,恢复。上面那段代码设置的样式不会影响其他  
  88.     CGContextRestoreGState(ctx);  
  89.       
  90.     //裁剪圆形头像  
  91.     CGContextAddEllipseInRect(ctx, CGRectMake(150150100 , 100));  
  92.     //按照圆形剪裁出一个上下文区域,以后的内容就填充在这个圆形上下文中  
  93.     CGContextClip(ctx);  
  94.     UIImage *img1=[UIImage imageNamed:@"me"];  
  95.     [img1 drawAtPoint:CGPointMake(150150)];  




转载自:http://blog.csdn.net/weisubao/article/details/41282457?utm_source=tuicool&utm_medium=referral

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值