代码改变生活

Quartz 2D 图形上下文栈 矩阵 裁剪

Quartz 2D 图形上下文栈  矩阵

 

 1 //
 2 //  DJVIew.m
 3 //  图形上下文栈
 4 //
 5 //  Created by zjj on 15/6/30.
 6 //  Copyright (c) 2015年 zjj. All rights reserved.
 7 //
 8 
 9 #import "DJVIew.h"
10 
11 @implementation DJVIew
12 
13 - (void)drawRect:(CGRect)rect
14 {
15     CGContextRef ref = UIGraphicsGetCurrentContext();
16     // 将ref(当前图形上下文)拷贝一份放入图形上下文栈里
17     CGContextSaveGState(ref);
18     //矩阵操作 旋转
19     CGContextRotateCTM(ref, M_PI_4*0.3);
20     // 矩阵操作 界面上所有图案缩放
21     CGContextScaleCTM(ref, 0.5, 0.5);
22     //矩阵操作 平移
23     CGContextTranslateCTM(ref, 100, 100);
24     // 设置绘图状态
25     [[UIColor redColor]set];
26     CGContextSetLineWidth(ref, 10);
27     CGContextSetLineCap(ref, kCGLineCapRound);
28     
29     CGContextMoveToPoint(ref, 20, 35);
30     CGContextAddLineToPoint(ref, 88, 195);
31     CGContextAddEllipseInRect(ref, CGRectMake(100, 100, 100, 100));//
32     CGContextAddArc(ref,200, 200, 150, 0, M_PI*2, 0);//
33     CGContextStrokePath(ref);
34 //    // 设置绘图状态
35 //    [[UIColor blackColor]set];
36 //    CGContextSetLineWidth(ref, 1);
37 //    CGContextSetLineCap(ref, kCGLineCapButt);
38     // 将栈顶上下文出栈替换当前上下文
39     CGContextRestoreGState(ref);
40 
41     CGContextAddRect(ref, CGRectMake(200, 200, 88, 88));//
42     CGContextMoveToPoint(ref, 10, 50);
43     CGContextAddLineToPoint(ref, 266, 225);
44     CGContextStrokePath(ref);
45 }
46 
47 @end

 裁剪 如:任意图片裁剪为圆形头像等

 

 

posted on 2015-06-30 02:24  张大少。  阅读(288)  评论(0编辑  收藏  举报

导航

繁星纵变 智慧永恒