1画线
A:
CGContextSetRGBStrokeColor(context,1.0, 0.0, 0.0,1.0);设置画笔的颜色
CGContextSetLineWidth(context,10.0);设置画笔宽度
CGContextMoveToPoint(context,10.0, 30.0 + baseHeight);将笔移动到某一点
CGContextAddLineToPoint(context,310.0, 30.0 + baseHeight);画一条线到某一点
CGContextStrokePath(context);用画笔将以上路径画出来
CGPoint addLines[] ={
CGPointMake(10.0,90.0 + baseHeight),
CGPointMake(70.0,60.0 + baseHeight),
CGPointMake(130.0,90.0+ baseHeight),
CGPointMake(190.0,60.0+ baseHeight),
CGPointMake(250.0,90.0+ baseHeight),
CGPointMake(310.0,60.0+ baseHeight),
};添加点的数组
CGContextSetStrokeColorWithColor(context, [UIColor cyanColor].CGColor);设置画笔的颜色
CGContextSetLineWidth(context,3);设置画笔宽度
CGContextAddLines(context, addLines,sizeof(addLines)/sizeof(addLines[0]));在多个点之间画线
CGContextStrokePath(context);用画笔将以上路径画出来
CGPoint strokeSegments[] ={
CGPointMake(10.0,150.0+ baseHeight),
CGPointMake(70.0,120.0+ baseHeight),
CGPointMake(130.0,150.0+ baseHeight),
CGPointMake(190.0,120.0+ baseHeight),
CGPointMake(250.0,150.0+ baseHeight),
CGPointMake(310.0,120.0+ baseHeight),
};
CGContextSetLineWidth(context,5);
CGContextStrokeLineSegments(context, strokeSegments,sizeof(strokeSegments)/sizeof(strokeSegments[0]));
画奇数点的线
B
CGContextSetRGBStrokeColor(context,1.0, 1.0, 1.0, 1.0);
CGContextSaveGState(context);保存上下文 状态1
CGContextMoveToPoint(context,40.0, 30.0+baseHeight);
CGContextAddLineToPoint(context,280.0, 30.0 + baseHeight);
CGContextSetLineWidth(context,self.width);
CGContextSetLineCap(context,self.cap);设置线的两端的样式
CGContextStrokePath(context);画线
CGContextRestoreGState(context);恢复到状态1之前的状态
CGContextSaveGState(context);状态2
CGContextMoveToPoint(context,40.0, 190.0+baseHeight);
CGContextAddLineToPoint(context,160.0, 70.0 +baseHeight);
CGContextAddLineToPoint(context,280.0, 190.0 +baseHeight);
CGContextSetLineWidth(context,self.width);
CGContextSetLineCap(context,self.cap);设置线两端的样式
CGContextSetLineJoin(context,self.join);设置连接线的样式
CGContextStrokePath(context);
CGContextRestoreGState(context);恢复到状态2之前的状态
C
CGContextSetRGBStrokeColor(context,1.0,0.0,0.0,1.0);
CGContextSetLineDash(context,self.dashPhase,dashPattern,dashCount);//画虚线 设置线的相隔度信息
CGContextMoveToPoint(context,10.0, 20.0+baseHeight);
CGContextAddLineToPoint(context,310.0, 20.0+baseHeight);
CGContextMoveToPoint(context,160.0, 30.0+baseHeight);
CGContextAddLineToPoint(context,160.0, 130.0+baseHeight);
CGContextAddRect(context,CGRectMake(10.0,30.0+baseHeight,100.0, 100.0));画矩形
CGContextAddEllipseInRect(context,CGRectMake(210.0,30.0+baseHeight,100.0, 100.0));画矩形的内切圆
CGContextSetLineWidth(context,5.0);
CGContextStrokePath(context);
2 画多边形
A
CGContextSetRGBStrokeColor(context,1.0,1.0,1.0,1.0);画笔颜色
CGContextSetRGBFillColor(context,0.0, 0.0, 1.0,1.0);填充的颜色
CGContextSetLineWidth(context,2.0);画笔的宽度
CGContextAddRect(context,CGRectMake(30.0,30.0 +baseHeight,60.0, 60.0));画矩形
CGContextStrokePath(context);用画笔画矩形 边框是画笔颜色
CGContextStrokeRect(context,CGRectMake(30.0,120.0+baseHeight,60.0, 60.0));用画笔话举行
CGContextStrokeRectWithWidth(context,CGRectMake(30.0,210.0+baseHeight,60.0, 60.0), 10.0);
CGContextSaveGState(context);
CGContextSetRGBStrokeColor(context,1.0, 0.0, 0.0,1.0);
CGContextStrokeRectWithWidth(context,CGRectMake(30.0,210.0+baseHeight,60.0, 60.0), 2.0);
CGContextRestoreGState(context);
CGRect rects[] = {
CGRectMake(120.0,30.0+baseHeight,60.0, 60.0),
CGRectMake(120.0,120.0+baseHeight,60.0, 60.0),
CGRectMake(120.0,210.0+baseHeight,60.0, 60.0),
};
CGContextAddRects(context, rects,sizeof(rects)/sizeof(rects[0]));同时画多个矩形
CGContextStrokePath(context);
CGContextAddRect(context,CGRectMake(210.0,30.0+baseHeight,60.0, 60.0));
CGContextFillPath(context);使用填充色填充矩形
CGContextFillRect(context,CGRectMake(210.0,120.0+baseHeight,60.0, 60.0));填充矩形 相当于以上两步结合
B
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);笔的颜色
CGContextSetRGBFillColor(context,0.0, 0.0, 1.0,1.0);填充色
CGContextSetLineWidth(context,2.0);
CGPoint center;
center =CGPointMake(90.0,90.0);
CGContextMoveToPoint(context, center.x, center.y + 60.0+baseHeight);
for(int i =1; i < 5; ++i)
{
CGFloat x =60.0 * sinf(i *4.0 * M_PI /5.0);
CGFloat y =60.0 * cosf(i *4.0 * M_PI /5.0);
CGContextAddLineToPoint(context, center.x + x, center.y + y+baseHeight);
}
CGContextClosePath(context);结束画线 会自动的将最后一条路径连接
center =CGPointMake(210.0,90.0);
CGContextMoveToPoint(context, center.x, center.y + 60.0+baseHeight);
for(int i =1; i < 6; ++i)
{
CGFloat x =60.0 * sinf(i *2.0 * M_PI /6.0);
CGFloat y =60.0 * cosf(i *2.0 * M_PI /6.0);
CGContextAddLineToPoint(context, center.x + x, center.y + y+baseHeight);
}
CGContextClosePath(context);
CGContextDrawPath(context,self.drawingMode);绘画路径
路径绘画模式:
enum CGPathDrawingMode {
kCGPathFill,//非绕零规则 看非绕零与奇偶的blog
kCGPathEOFill,//奇偶规则
kCGPathStroke,
kCGPathFillStroke,
kCGPathEOFillStroke
};
3画圆形弧形
A
CGContextSetRGBStrokeColor(context,1.0,1.0,1.0,1.0);
CGContextSetRGBFillColor(context,1.0, 0.0, 0.0,1.0);
CGContextSetLineWidth(context,2.0);
CGContextAddEllipseInRect(context,CGRectMake(30.0,30.0+baseHeight,60.0, 60.0));画椭圆 矩形内切圆
CGContextStrokePath(context);
CGContextStrokeEllipseInRect(context,CGRectMake(30.0,120.0+baseHeight,60.0, 60.0));画笔画椭圆
CGContextFillEllipseInRect(context,CGRectMake(30.0,210.0+baseHeight,60.0, 60.0));填充椭圆
CGContextAddArc(context,150.0, 60.0+baseHeight,30.0, 0.0, M_PI/2, false);画圆弧 false 逆时针方向
CGContextStrokePath(context);
CGContextAddArc(context,150.0, 60.0+baseHeight,30.0,3.0*M_PI/2.0,M_PI, true);ture 顺时针方向
CGContextStrokePath(context);
CGContextAddArc(context,150.0, 150.0+baseHeight,30.0, 0.0, M_PI/2.0, false);
CGContextAddArc(context,150.0, 150.0+baseHeight,30.0, 3.0*M_PI/2.0,M_PI, true);
CGContextStrokePath(context);中间线相连
CGContextAddArc(context,150.0, 240.0+baseHeight,30.0, 0.0, M_PI/2.0, false);
CGContextAddArc(context,150.0, 240.0+baseHeight,30.0, M_PI,3.0*M_PI/2.0,false);
CGContextStrokePath(context);
// Stroke an arc using AddArcToPoint
CGPoint p[3] ={
CGPointMake(210.0,30.0+baseHeight),
CGPointMake(210.0,60.0+baseHeight),
CGPointMake(240.0,60.0+baseHeight),
};
CGContextMoveToPoint(context, p[0].x, p[0].y);
CGContextAddArcToPoint(context, p[1].x, p[1].y, p[2].x, p[2].y,30.0);以半径 画两点的弧线
CGContextStrokePath(context);
CGContextSetRGBStrokeColor(context,1.0, 0.0, 0.0,1.0);
CGContextAddLines(context, p,sizeof(p)/sizeof(p[0]));
CGContextStrokePath(context);画直线
CGContextSetRGBStrokeColor(context,1.0, 1.0, 1.0,1.0);
CGRect rrect =CGRectMake(210.0,90.0+baseHeight,60.0, 60.0);
CGFloat radius =10.0;
CGFloat minx =CGRectGetMinX(rrect), midx = CGRectGetMidX(rrect), maxx = CGRectGetMaxX(rrect);
CGFloat miny =CGRectGetMinY(rrect), midy = CGRectGetMidY(rrect), maxy = CGRectGetMaxY(rrect);
CGContextMoveToPoint(context, minx, midy);
// Add an arc through 2 to 3
CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
// Add an arc through 4 to 5
CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
// Add an arc through 6 to 7
CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
// Add an arc through 8 to 9
CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius);
// Close the path
CGContextClosePath(context);
// Fill & stroke the path
CGContextDrawPath(context,kCGPathFillStroke);绘画路径 绘画线并且填充颜色
4 用图片画图{
CGRect imageRect;
imageRect.origin =CGPointMake(8.0,8.0+baseHeight);
imageRect.size =CGSizeMake(64.0,64.0);
CGContextDrawImage(context, imageRect,self.image);将图片画在矩形内
CGContextClipToRect(context,CGRectMake(0.0,80.0+baseHeight,self.bounds.size.width,self.bounds.size.height));
imageRect.origin =CGPointMake(32.0,112.0+baseHeight);
CGContextDrawTiledImage(context, imageRect,self.image);
CGContextSetRGBFillColor(context,1.0, 0.0, 0.0,0.5);
CGContextFillRect(context, imageRect);
// And stroke the clipped area
CGContextSetLineWidth(context,10.0);
CGContextSetRGBStrokeColor(context,1.0, 0.0, 0.0,1.0);
CGContextStrokeRect(context,CGContextGetClipBoundingBox(context));
}
- (CGImageRef)image{
if (_image ==NULL){
NSString *imagePath = [[NSBundlemainBundle] pathForResource:@"Demo"ofType:@"png"];
UIImage *img = [UIImageimageWithContentsOfFile:imagePath];
_image =CGImageRetain(img.CGImage);
}
return_image;
}
2
PDF文档{
CGContextTranslateCTM(context,0.0, self.bounds.size.height);以屏幕的高度 调换坐标系 iOS LU 00 OSLD :00
CGContextScaleCTM(context,1.0, -1.0);y的负方向
CGPDFPageRef page =CGPDFDocumentGetPage(self.pdfDocument,1);获取pdf的页数信息
CGContextSaveGState(context);
CGAffineTransform pdfTransform =CGPDFPageGetDrawingTransform(page, kCGPDFCropBox,self.bounds,0,true);
CGContextConcatCTM(context, pdfTransform);
CGContextDrawPDFPage(context, page);绘画pdf
CGContextRestoreGState(context);
}
-(CGPDFDocumentRef)pdfDocument{
if (_pdfDocument ==NULL){
CFURLRef pdfURL =CFBundleCopyResourceURL(CFBundleGetMainBundle(),CFSTR("Quartz.pdf"),NULL, NULL);
_pdfDocument =CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
CFRelease(pdfURL);
}
return_pdfDocument;
}
3绘画文本
CGContextSetRGBFillColor(context,1.0, 1.0, 1.0, 1.0);
CGContextSetRGBStrokeColor(context,1.0, 0.0, 0.0,1.0);
CGContextSelectFont(context,"Helvetica",36.0, kCGEncodingMacRoman);设置字体
CGContextSetTextMatrix(context,CGAffineTransformMakeScale(1.0, -1.0));转换矩阵
CGContextSetTextDrawingMode(context,kCGTextFill);绘画模式
CGContextShowTextAtPoint(context,10.0, 30.0+baseHeight,kTextString, kTextStringLength);
CGContextSetTextDrawingMode(context,kCGTextStroke);
CGContextShowTextAtPoint(context,10.0, 70.0+baseHeight,kTextString, kTextStringLength);
CGContextSetTextDrawingMode(context,kCGTextFillStroke);
CGContextShowTextAtPoint(context,10.0, 110.0+baseHeight,kTextString, kTextStringLength);