<strong><span style="font-size:14px;">
</span></strong>
<strong><span style="font-size:14px;">
</span></strong>
<strong><span style="font-size:14px;">
</span></strong>
<strong><span style="font-size:14px;">- (void)drawRect:(CGRect)rect
{
//1.画文字
NSString* data = @"Quartz2D的使用";
// 调用Draw方法
[data drawInRect:CGRectMake(100, 0, 100, 100) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];
//2.画图片
UIImage* image = [UIImage imageNamed:@"Volibear"];
// 利用drawInRect方法绘制图片到layer, 是通过拉伸原有图片
[image drawInRect:CGRectMake(100, 50, 100, 100)];
// 平铺模式显示按照片大小
[image drawAsPatternInRect:CGRectMake(0, 160, 320, 100)];
}</span></strong>
一、绘图路径
A.简单说明
在画线的时候,方法的内部默认创建一个path。它把路径都放到了path里面去。
1.创建路径 cgmutablepathref 调用该方法相当于创建了一个路径,这个路径用来保存绘图信息。
2.把绘图信息添加到路径里边。
以前的方法是点的位置添加到ctx(图形上下文信息)中,ctx 默认会在内部创建一个path用来保存绘图信息。
在图形上下文中有一块存储空间专门用来存储绘图信息,其实这块空间就是CGMutablePathRef。
3.把路径添加到上下文中。
代码示例:
绘制一条直线的代码:
<strong><span style="font-size:14px;"> /*
// 方式 A
// 获得上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 设置起点
CGContextMoveToPoint(ctx, 50, 50);
// 设置线条
CGContextAddLineToPoint(ctx, 200, 200);
// 线宽
CGContextSetLineWidth(ctx, 10);
// 渲染
CGContextStrokePath(ctx);
*/
// 方式 B
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 创建可变路径
CGMutablePathRef path = CGPathCreateMutable();
// 绘制一个圆
CGPathAddEllipseInRect(path, nil, CGRectMake(100, 100, 100, 100));
// 将路径添加到上下文
CGContextAddPath(ctx, path);
// 渲染
CGContextFillPath(ctx);</span></strong>
<strong><span style="font-size:14px;"><img src="https://img-blog.csdn.net/20160123175724654?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
</span></strong>
<span style="font-size:14px;"></span><p class="p1" style="font-weight: bold;"><span class="s1"> </span><span class="s2">// </span><span class="s3">通过</span><span class="s2">Quartz2D</span><span class="s3">中带有</span><span class="s2">creat/copy/retain</span><span class="s3">方法创建出来的值都必须要释放</span></p><p class="p2"><span class="s1"> </span><span class="s2">CGPathRelease</span><span class="s1">(path);</span></p><p class="p2"><span class="s1"></span></p><p class="p1"><span class="s1"> B.</span><span class="s2">直接使用</span><span class="s1">path</span><span class="s2">的好处:</span></p><p class="p2"><span class="s3"> </span><span class="s1">第一种代码的阅读性不好,不便于区分。使用</span><span class="s3">path</span><span class="s1">,则一个</span><span class="s3">path</span><span class="s1">就代表一条路径。</span></p><p class="p2"><span class="s3"> </span><span class="s1">比如:如果要在上下文中绘制多个图形,这种情况下建议使用</span><span class="s3">path</span><span class="s1">。</span></p><p class="p1"><span class="s1"> </span><span class="s2">代码示例:</span></p><p class="p1"><span class="s2"></span><pre name="code" class="objc"> //1.获取图形上下文
CGContextRef ctx=UIGraphicsGetCurrentContext();
//2.绘图
//2.a 画一条直线
//2.a.1创建一条绘图的路径
//注意:但凡通过Quartz2D中带有creat/copy/retain方法创建出来的值都必须要释放
CGMutablePathRef path=CGPathCreateMutable();
//2.a.2把绘图信息添加到路径里
CGPathMoveToPoint(path, NULL, 20, 20);
CGPathAddLineToPoint(path, NULL, 200, 300);
//2.a.3把路径添加到上下文中
//把绘制直线的绘图信息保存到图形上下文中
CGContextAddPath(ctx, path);
//2.b画一个圆
//2.b.1创建一条画圆的绘图路径(注意这里是可变的,不是CGPathRef)
CGMutablePathRef path1=CGPathCreateMutable();
//2.b.2把圆的绘图信息添加到路径里
CGPathAddEllipseInRect(path1, NULL, CGRectMake(50, 50, 100, 100));
//2.b.3把圆的路径添加到图形上下文中
CGContextAddPath(ctx, path1);
//3.渲染
CGContextStrokePath(ctx);
//4.释放前面创建的两条路径
//第一种方法
CGPathRelease(path);
CGPathRelease(path1);
//第二种方法
// CFRelease(path);