ios绘图之quarz2d

 1 - (void)drawRect:(CGRect)rect
 2 {
 3     // 1.获取上下文
 4     CGContextRef ctx = UIGraphicsGetCurrentContext();
 5    
 6     // 2.创建路径(一个path就代表一条路径)
 7     // 但凡通过quarzt2d中的带有create/ copy / retain 方法创建出来的值都必须手动的释放
 8     CGMutablePathRef path = CGPathCreateMutable();
 9     // 设置起点
10     CGPathMoveToPoint(path, NULL, 10, 10);
11     // 设置终点
12     CGPathAddLineToPoint(path, NULL, 100, 100);
13     // 将路径添加到上下文中
14     CGContextAddPath(ctx, path);
15     
16     // 3.再创建一条路径用于保存圆
17      CGMutablePathRef path2 = CGPathCreateMutable();
18     // 在path中添加画的路径
19     CGPathAddEllipseInRect(path2, NULL, CGRectMake(50, 50, 50, 50));
20     CGContextAddPath(ctx, path2);
21     
22     // 3.渲染'
23     CGContextStrokePath(ctx);
24     
25     
26     // 释放前面创建的两条路径
27     CGPathRelease(path);
28     CGPathRelease(path2);
29     
30     // 下面这种方式也可以释放路径
31     
32 //    CFRelease(path);
33 //    CFRelease(path2);
34 }

注意:画完后要释放资源

posted @ 2016-01-23 11:48  xiaocaoera  阅读(203)  评论(0编辑  收藏  举报