Quartz 各种绘制图形用法 Quartz 实现画图片、写文字、画线、椭圆、矩形、棱形等。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- ( void )drawRect:(CGRect)rect
{
     CGContextRef context = UIGraphicsGetCurrentContext();
     
 
     
     /*NO.1画一条线
      
      CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5);//线条颜色
      CGContextMoveToPoint(context, 20, 20);
      CGContextAddLineToPoint(context, 200,20);
      CGContextStrokePath(context);
     */
 
     
     
     /*NO.2写文字
      
     CGContextSetLineWidth(context, 1.0);
     CGContextSetRGBFillColor (context, 0.5, 0.5, 0.5, 0.5);
     UIFont  *font = [UIFont boldSystemFontOfSize:18.0];
     [@"公司:北京中软科技股份有限公司\n部门:ERP事业部\n姓名:McLiang" drawInRect:CGRectMake(20, 40, 280, 300) withFont:font];
     */
 
     
     /*NO.3画一个正方形图形 没有边框
 
     CGContextSetRGBFillColor(context, 0, 0.25, 0, 0.5);
     CGContextFillRect(context, CGRectMake(2, 2, 270, 270));
     CGContextStrokePath(context);
     */
  
     
     /*NO.4画正方形边框
     
     CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5);//线条颜色
     CGContextSetLineWidth(context, 2.0);
     CGContextAddRect(context, CGRectMake(2, 2, 270, 270));
     CGContextStrokePath(context);
     */
 
     
     /*NO.5画方形背景颜色
      
     CGContextTranslateCTM(context, 0.0f, self.bounds.size.height);
     CGContextScaleCTM(context, 1.0f, -1.0f);
     UIGraphicsPushContext(context);
     CGContextSetLineWidth(context,320);
     CGContextSetRGBStrokeColor(context, 250.0/255, 250.0/255, 210.0/255, 1.0);
     CGContextStrokeRect(context, CGRectMake(0, 0, 320, 460));
     UIGraphicsPopContext();
     */
 
     /*NO.6椭圆
      
      CGRect aRect= CGRectMake(80, 80, 160, 100);
      CGContextSetRGBStrokeColor(context, 0.6, 0.9, 0, 1.0);
      CGContextSetLineWidth(context, 3.0);
      CGContextAddEllipseInRect(context, aRect); //椭圆
      CGContextDrawPath(context, kCGPathStroke);
     */
 
     /*NO.7
     CGContextBeginPath(context);
     CGContextSetRGBStrokeColor(context, 0, 0, 1, 1);
     CGContextMoveToPoint(context, 100, 100);
     CGContextAddArcToPoint(context, 50, 100, 50, 150, 50);
     CGContextStrokePath(context);
     */
 
     /*NO.8渐变
     CGContextClip(context);
     CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
     CGFloat colors[] =
     {
         204.0 / 255.0, 224.0 / 255.0, 244.0 / 255.0, 1.00,
         29.0 / 255.0, 156.0 / 255.0, 215.0 / 255.0, 1.00,
         0.0 / 255.0,  50.0 / 255.0, 126.0 / 255.0, 1.00,
     };
     CGGradientRef gradient = CGGradientCreateWithColorComponents
     (rgb, colors, NULL, sizeof(colors)/(sizeof(colors[0])*4));
     CGColorSpaceRelease(rgb);
     CGContextDrawLinearGradient(context, gradient,CGPointMake
                                 (0.0,0.0) ,CGPointMake(0.0,self.frame.size.height),
                                 kCGGradientDrawsBeforeStartLocation);
      */
     
    
     /* NO.9四条线画一个正方形
     //画线
         UIColor *aColor = [UIColor colorWithRed:0 green:1.0 blue:0 alpha:0];
     CGContextSetRGBStrokeColor(context, 1.0, 0, 0, 1.0);
        CGContextSetFillColorWithColor(context, aColor.CGColor);
     CGContextSetLineWidth(context, 4.0);
     CGPoint aPoints[5];
     aPoints[0] =CGPointMake(60, 60);
     aPoints[1] =CGPointMake(260, 60);
     aPoints[2] =CGPointMake(260, 300);
     aPoints[3] =CGPointMake(60, 300);
     aPoints[4] =CGPointMake(60, 60);
     CGContextAddLines(context, aPoints, 5);
     CGContextDrawPath(context, kCGPathStroke); //开始画线
      */
     
     
     
     /*  NO.10
     UIColor *aColor = [UIColor colorWithRed:0 green:1.0 blue:0 alpha:0];
     CGContextSetRGBStrokeColor(context, 1.0, 0, 0, 1.0);
     CGContextSetFillColorWithColor(context, aColor.CGColor);
     //椭圆
     CGRect aRect= CGRectMake(80, 80, 160, 100);
     CGContextSetRGBStrokeColor(context, 0.6, 0.9, 0, 1.0);
     CGContextSetLineWidth(context, 3.0);
       CGContextSetFillColorWithColor(context, aColor.CGColor);
        CGContextAddRect(context, rect); //矩形
     CGContextAddEllipseInRect(context, aRect); //椭圆
     CGContextDrawPath(context, kCGPathStroke);
      */
 
     
     
     /*  NO.11
      画一个实心的圆
  
      CGContextFillEllipseInRect(context, CGRectMake(95, 95, 100.0, 100));
     */
     
     
     
     /*NO.12
      画一个菱形
     CGContextSetLineWidth(context, 2.0);
     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
     CGContextMoveToPoint(context, 100, 100);
     CGContextAddLineToPoint(context, 150, 150);
     CGContextAddLineToPoint(context, 100, 200);
     CGContextAddLineToPoint(context, 50, 150);
     CGContextAddLineToPoint(context, 100, 100);
     CGContextStrokePath(context);
      */
 
     /*NO.13 画矩形
     CGContextSetLineWidth(context, 2.0);
 
     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
 
     CGRect rectangle = CGRectMake(60,170,200,80);
 
     CGContextAddRect(context, rectangle);
     
     CGContextStrokePath(context);
      */
     
    
     /*椭圆
     CGContextSetLineWidth(context, 2.0);
 
     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
 
     CGRect rectangle = CGRectMake(60,170,200,80);
 
     CGContextAddEllipseInRect(context, rectangle);
     
     CGContextStrokePath(context);
      */
     
     /*用红色填充了一段路径:
     
     CGContextMoveToPoint(context, 100, 100);
     CGContextAddLineToPoint(context, 150, 150);
     CGContextAddLineToPoint(context, 100, 200);
     CGContextAddLineToPoint(context, 50, 150);
     CGContextAddLineToPoint(context, 100, 100);
     CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
     CGContextFillPath(context);
     */
     
     /*填充一个蓝色边的红色矩形
     CGContextSetLineWidth(context, 2.0);
     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
     CGRect rectangle = CGRectMake(60,170,200,80);
     CGContextAddRect(context, rectangle);
     CGContextStrokePath(context);
     CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
     CGContextFillRect(context, rectangle);
     */
     
     /*画弧
      //弧线的是通过指定两个切点,还有角度,调用CGContextAddArcToPoint()绘制
     CGContextSetLineWidth(context, 2.0);
     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
     CGContextMoveToPoint(context, 100, 100);
     CGContextAddArcToPoint(context, 100,200, 300,200, 100);
     CGContextStrokePath(context);
     */
    
     
     /*
     绘制贝兹曲线
     //贝兹曲线是通过移动一个起始点,然后通过两个控制点,还有一个中止点,调用CGContextAddCurveToPoint() 函数绘制
     CGContextSetLineWidth(context, 2.0);
 
     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
 
     CGContextMoveToPoint(context, 10, 10);
 
     CGContextAddCurveToPoint(context, 0, 50, 300, 250, 300, 400);
     
     CGContextStrokePath(context);
      */
     
     /*绘制二次贝兹曲线
     
       CGContextSetLineWidth(context, 2.0);
 
       CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
 
       CGContextMoveToPoint(context, 10, 200);
 
       CGContextAddQuadCurveToPoint(context, 150, 10, 300, 200);
     
       CGContextStrokePath(context);
      */
     
     /*绘制虚线
     CGContextSetLineWidth(context, 5.0);
 
     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
 
     CGFloat dashArray[] = {2,6,4,2};
 
     CGContextSetLineDash(context, 3, dashArray, 4);//跳过3个再画虚线,所以刚开始有6-(3-2)=5个虚点
     
     CGContextMoveToPoint(context, 10, 200);
     
     CGContextAddQuadCurveToPoint(context, 150, 10, 300, 200);
     
     CGContextStrokePath(context);
     */
/*绘制图片
     NSString* imagePath = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"];
     UIImage* myImageObj = [[UIImage alloc] initWithContentsOfFile:imagePath];
     //[myImageObj drawAtPoint:CGPointMake(0, 0)];
     [myImageObj drawInRect:CGRectMake(0, 0, 320, 480)];
 
     NSString *s = @"我的小狗";
 
     [s drawAtPoint:CGPointMake(100, 0) withFont:[UIFont systemFontOfSize:34.0]];
*/
     
   /*
     NSString *path = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"];
     UIImage *img = [UIImage imageWithContentsOfFile:path];
     CGImageRef image = img.CGImage;
     CGContextSaveGState(context);
     CGRect touchRect = CGRectMake(0, 0, img.size.width, img.size.height);
     CGContextDrawImage(context, touchRect, image);
     CGContextRestoreGState(context);
    */
   
     
     /*NSString *path = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"];
     UIImage *img = [UIImage imageWithContentsOfFile:path];
     CGImageRef image = img.CGImage;
     CGContextSaveGState(context);
 
     CGContextRotateCTM(context, M_PI);
     CGContextTranslateCTM(context, -img.size.width, -img.size.height);
 
     CGRect touchRect = CGRectMake(0, 0, img.size.width, img.size.height);
     CGContextDrawImage(context, touchRect, image);
     CGContextRestoreGState(context);*/
 
/*
     NSString *path = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"];
     UIImage *img = [UIImage imageWithContentsOfFile:path];
     CGImageRef image = img.CGImage;
     
     CGContextSaveGState(context);
 
     CGAffineTransform myAffine = CGAffineTransformMakeRotation(M_PI);
     myAffine = CGAffineTransformTranslate(myAffine, -img.size.width, -img.size.height);
     CGContextConcatCTM(context, myAffine);
 
     CGContextRotateCTM(context, M_PI);
     CGContextTranslateCTM(context, -img.size.width, -img.size.height);
 
     CGRect touchRect = CGRectMake(0, 0, img.size.width, img.size.height);
     CGContextDrawImage(context, touchRect, image);
     CGContextRestoreGState(context);
*/

猜你喜欢

转载自blog.csdn.net/liulushi_1988/article/details/8524598