首页 技术 正文
技术 2022年11月15日
0 收藏 975 点赞 3,552 浏览 2593 个字

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

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

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

  1. – (void)drawRect:(CGRect)rect {
  2. //获得当前上下文
  3. CGContextRef ctx=UIGraphicsGetCurrentContext();
  4. //把当前上下文状态保存在栈中
  5. CGContextSaveGState(ctx);
  6. //缩放、移动处理(需要放在画图之前进行设置)
  7. CGContextScaleCTM(ctx, 0.5, 0.5);
  8. CGContextTranslateCTM(ctx, 100, 100);
  9. CGContextRotateCTM(ctx, M_PI_4);
  10. //描点
  11. CGContextMoveToPoint(ctx, 10, 10);
  12. CGContextAddLineToPoint(ctx, 100, 100);
  13. CGContextAddLineToPoint(ctx, 150, 50);
  14. //以下两种方式均可闭环
  15. //CGContextAddLineToPoint(ctx, 10, 10);
  16. CGContextClosePath(ctx);
  17. //渲染绘图,实心和空心
  18. CGContextStrokePath(ctx);
  19. //CGContextFillPath(ctx);
  20. //把当前上下文状态保存在栈中
  21. CGContextSaveGState(ctx);
  22. //画正方形
  23. CGContextAddRect(ctx, CGRectMake(100, 100, 50, 50));
  24. //设置线宽(一定要在CGContextStrokePath之前)
  25. //因为之前有过一次渲染绘图,所以这个属性设置不影响上面的那个三角形,以下颜色设置同理
  26. //所以,如果想分别设置两个或多个图形的属性,就分别渲染绘图一次
  27. CGContextSetLineWidth(ctx, 10);
  28. //设置颜色(同理,属性设置的代码都要在绘图的代码之前)
  29. CGContextSetRGBStrokeColor(ctx, 1, 0, 0, 1);
  30. CGContextStrokePath(ctx);
  31. //设置样式
  32. CGContextMoveToPoint(ctx, 20, 160);
  33. CGContextAddLineToPoint(ctx, 200, 280);
  34. CGContextAddLineToPoint(ctx, 250, 200);
  35. CGContextSetLineWidth(ctx, 20);
  36. //设置头尾样式
  37. CGContextSetLineCap(ctx, kCGLineCapRound);
  38. //设置转角样式
  39. CGContextSetLineJoin(ctx, kCGLineJoinRound);
  40. CGContextStrokePath(ctx);
  41. //把保存在栈中的上下文状态取出来,恢复。上面那段代码设置的样式不会影响其他
  42. CGContextRestoreGState(ctx);
  43. //画椭圆
  44. CGContextAddEllipseInRect(ctx, CGRectMake(200, 130, 60, 30));
  45. //以下等价
  46. //CGContextStrokePath(ctx);
  47. CGContextDrawPath(ctx, kCGPathStroke);
  48. //画圆形
  49. CGContextAddEllipseInRect(ctx, CGRectMake(140, 170, 50, 50));
  50. CGContextSetLineWidth(ctx, 3);
  51. CGContextStrokePath(ctx);
  52. //画圆弧
  53. CGContextAddArc(ctx, 200, 50, 50, M_PI_4, M_PI, 1);
  54. CGContextStrokePath(ctx);
  55. //画1/4圆,以及颜色的设置新方法
  56. CGContextMoveToPoint(ctx, 10, 230);
  57. CGContextAddLineToPoint(ctx, 10, 280);
  58. CGContextAddLineToPoint(ctx, 60, 280);
  59. CGContextAddArc(ctx, 10, 280, 50, 0, -M_PI_2, 1);
  60. [[UIColor greenColor] setStroke];
  61. CGContextStrokePath(ctx);
  62. //画图片和文字(不需要手动取得上下文)
  63. NSString *str1=@”辛丑年一空作”;
  64. [str1 drawAtPoint:CGPointZero withAttributes:nil];
  65. UIImage *img=[UIImage imageNamed:@”001″];
  66. [img drawAtPoint:CGPointMake(10, 10)];
  67. //在一个框框里重叠图片并署名
  68. CGRect rect1=CGRectMake(50, 50, 100, 100);
  69. [img drawAsPatternInRect:rect1];
  70. NSMutableDictionary *attr=[[NSMutableDictionary alloc]init];
  71. attr[NSForegroundColorAttributeName]=[UIColor whiteColor];
  72. attr[NSFontAttributeName]=[UIFont systemFontOfSize:13];
  73. [str1 drawInRect:CGRectMake(50, 140, 100, 100) withAttributes:attr];
  74. //把保存在栈中的上下文状态取出来,恢复。上面那段代码设置的样式不会影响其他
  75. CGContextRestoreGState(ctx);
  76. //裁剪圆形头像
  77. CGContextAddEllipseInRect(ctx, CGRectMake(150, 150, 100 , 100));
  78. //按照圆形剪裁出一个上下文区域,以后的内容就填充在这个圆形上下文中
  79. CGContextClip(ctx);
  80. UIImage *img1=[UIImage imageNamed:@”me”];
  81. [img1 drawAtPoint:CGPointMake(150, 150)];
  82. }
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289