# DrawRect **Repository Path**: UIControl_admin/DrawRect ## Basic Information - **Project Name**: DrawRect - **Description**: 画线 - **Primary Language**: Objective-C - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 3 - **Forks**: 0 - **Created**: 2016-05-04 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 画线 大神博客:http://www.cnblogs.com/kenshincui/p/3959951.html 第三方库:PNChart 第三方网址:https://github.com/kevinzhow/PNChart //drawRect:系统提供给我们进行自定义绘制的方法。 /** * 绘图步骤 * ① 获得绘制的上下文,即画布 CGContextRef context = UIGraphicsGetCurrentContext(); * ② 设置绘制的属性 CGContextSetLineWidth(context, 5.0f); * ③ 添加需要绘制的图形 CGContextAddRect(context, CGRectMake(100, 60, 200, 100)); * ④ 绘制所有的内容 CGContextStrokePath(context); */ 1. - (void)drawRect:(CGRect)rect   2. {   3.     CGContextRef context = UIGraphicsGetCurrentContext();   4.         5.     6.         7.     /*NO.1画一条线  8.         9.      CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5);//线条颜色  10.      CGContextMoveToPoint(context, 20, 20);  11.      CGContextAddLineToPoint(context, 200,20);  12.      CGContextStrokePath(context);  13.     */   14.     15.         16.         17.     /*NO.2写文字  18.         19.     CGContextSetLineWidth(context, 1.0);  20.     CGContextSetRGBFillColor (context, 0.5, 0.5, 0.5, 0.5);  21.     UIFont  *font = [UIFont boldSystemFontOfSize:18.0];  22.     [@"公司:北京中软科技股份有限公司\n部门:ERP事业部\n姓名:McLiang" drawInRect:CGRectMake(20, 40, 280, 300) withFont:font];  23.     */   24.     25.         26.     /*NO.3画一个正方形图形 没有边框  27.    28.     CGContextSetRGBFillColor(context, 0, 0.25, 0, 0.5);  29.     CGContextFillRect(context, CGRectMake(2, 2, 270, 270));  30.     CGContextStrokePath(context);  31.     */   32.      33.         34.     /*NO.4画正方形边框  35.        36.     CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5);//线条颜色  37.     CGContextSetLineWidth(context, 2.0);  38.     CGContextAddRect(context, CGRectMake(2, 2, 270, 270));  39.     CGContextStrokePath(context);  40.     */   41.     42.         43.     /*NO.5画方形背景颜色  44.         45.     CGContextTranslateCTM(context, 0.0f, self.bounds.size.height);  46.     CGContextScaleCTM(context, 1.0f, -1.0f);  47.     UIGraphicsPushContext(context);  48.     CGContextSetLineWidth(context,320);  49.     CGContextSetRGBStrokeColor(context, 250.0/255, 250.0/255, 210.0/255, 1.0);  50.     CGContextStrokeRect(context, CGRectMake(0, 0, 320, 460));  51.     UIGraphicsPopContext();  52.     */   53.     54.     /*NO.6椭圆  55.         56.      CGRect aRect= CGRectMake(80, 80, 160, 100);  57.      CGContextSetRGBStrokeColor(context, 0.6, 0.9, 0, 1.0);  58.      CGContextSetLineWidth(context, 3.0);  59.      CGContextAddEllipseInRect(context, aRect); //椭圆  60.      CGContextDrawPath(context, kCGPathStroke);  61.     */   62.     63.     /*NO.7  圆 64.     CGContextBeginPath(context);  65.     CGContextSetRGBStrokeColor(context, 0, 0, 1, 1);  66.     CGContextMoveToPoint(context, 100, 100);  67.     CGContextAddArcToPoint(context, 50, 100, 50, 150, 50);  68.     CGContextStrokePath(context);  69.     */   70.     71.     /*NO.8渐变  72.     CGContextClip(context);  73.     CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();  74.     CGFloat colors[] =  75.     {  76.         204.0 / 255.0, 224.0 / 255.0, 244.0 / 255.0, 1.00,  77.         29.0 / 255.0, 156.0 / 255.0, 215.0 / 255.0, 1.00,  78.         0.0 / 255.0,  50.0 / 255.0, 126.0 / 255.0, 1.00,  79.     };  80.     CGGradientRef gradient = CGGradientCreateWithColorComponents  81.     (rgb, colors, NULL, sizeof(colors)/(sizeof(colors[0])*4));  82.     CGColorSpaceRelease(rgb);  83.     CGContextDrawLinearGradient(context, gradient,CGPointMake  84.                                 (0.0,0.0) ,CGPointMake(0.0,self.frame.size.height),  85.                                 kCGGradientDrawsBeforeStartLocation);  86.      */   87.         88.        89.     /* NO.9四条线画一个正方形  90.     //画线  91.         UIColor *aColor = [UIColor colorWithRed:0 green:1.0 blue:0 alpha:0];  92.     CGContextSetRGBStrokeColor(context, 1.0, 0, 0, 1.0);  93.        CGContextSetFillColorWithColor(context, aColor.CGColor);  94.     CGContextSetLineWidth(context, 4.0);  95.     CGPoint aPoints[5];  96.     aPoints[0] =CGPointMake(60, 60);  97.     aPoints[1] =CGPointMake(260, 60);  98.     aPoints[2] =CGPointMake(260, 300);  99.     aPoints[3] =CGPointMake(60, 300);  100.     aPoints[4] =CGPointMake(60, 60);  101.     CGContextAddLines(context, aPoints, 5);  102.     CGContextDrawPath(context, kCGPathStroke); //开始画线  103.      */   104.         105.         106.         107.     /*  NO.10  108.     UIColor *aColor = [UIColor colorWithRed:0 green:1.0 blue:0 alpha:0];  109.     CGContextSetRGBStrokeColor(context, 1.0, 0, 0, 1.0);  110.     CGContextSetFillColorWithColor(context, aColor.CGColor);  111.     //椭圆  112.     CGRect aRect= CGRectMake(80, 80, 160, 100);  113.     CGContextSetRGBStrokeColor(context, 0.6, 0.9, 0, 1.0);  114.     CGContextSetLineWidth(context, 3.0);  115.       CGContextSetFillColorWithColor(context, aColor.CGColor);  116.        CGContextAddRect(context, rect); //矩形  117.     CGContextAddEllipseInRect(context, aRect); //椭圆  118.     CGContextDrawPath(context, kCGPathStroke);  119.      */   120.     121.         122.         123.     /*  NO.11  124.      画一个实心的圆  125.     126.      CGContextFillEllipseInRect(context, CGRectMake(95, 95, 100.0, 100));  127.     */   128.         129.         130.         131.     /*NO.12  132.      画一个菱形  133.     CGContextSetLineWidth(context, 2.0);  134.     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);  135.     CGContextMoveToPoint(context, 100, 100);  136.     CGContextAddLineToPoint(context, 150, 150);  137.     CGContextAddLineToPoint(context, 100, 200);  138.     CGContextAddLineToPoint(context, 50, 150);  139.     CGContextAddLineToPoint(context, 100, 100);  140.     CGContextStrokePath(context);  141.      */   142.     143.     /*NO.13 画矩形  144.     CGContextSetLineWidth(context, 2.0);  145.    146.     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);  147.    148.     CGRect rectangle = CGRectMake(60,170,200,80);  149.    150.     CGContextAddRect(context, rectangle);  151.        152.     CGContextStrokePath(context);  153.      */   154.         155.        156.     /*椭圆  157.     CGContextSetLineWidth(context, 2.0);  158.    159.     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);  160.    161.     CGRect rectangle = CGRectMake(60,170,200,80);  162.    163.     CGContextAddEllipseInRect(context, rectangle);  164.        165.     CGContextStrokePath(context);  166.      */   167.         168.     /*用红色填充了一段路径:  169.        170.     CGContextMoveToPoint(context, 100, 100);  171.     CGContextAddLineToPoint(context, 150, 150);  172.     CGContextAddLineToPoint(context, 100, 200);  173.     CGContextAddLineToPoint(context, 50, 150);  174.     CGContextAddLineToPoint(context, 100, 100);  175.     CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);  176.     CGContextFillPath(context);  177.     */   178.         179.     /*填充一个蓝色边的红色矩形  180.     CGContextSetLineWidth(context, 2.0);  181.     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);  182.     CGRect rectangle = CGRectMake(60,170,200,80);  183.     CGContextAddRect(context, rectangle);  184.     CGContextStrokePath(context);  185.     CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);  186.     CGContextFillRect(context, rectangle);  187.     */   188.         189.     /*画弧  190.      //弧线的是通过指定两个切点,还有角度,调用CGContextAddArcToPoint()绘制  191.     CGContextSetLineWidth(context, 2.0);  192.     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);  193.     CGContextMoveToPoint(context, 100, 100);  194.     CGContextAddArcToPoint(context, 100,200, 300,200, 100);  195.     CGContextStrokePath(context);  196.     */   197.        198.         199.     /*  200.     绘制贝兹曲线  201.     //贝兹曲线是通过移动一个起始点,然后通过两个控制点,还有一个中止点,调用CGContextAddCurveToPoint() 函数绘制  202.     CGContextSetLineWidth(context, 2.0);  203.    204.     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);  205.    206.     CGContextMoveToPoint(context, 10, 10);  207.    208.     CGContextAddCurveToPoint(context, 0, 50, 300, 250, 300, 400);  209.        210.     CGContextStrokePath(context);  211.      */   212.         213.     /*绘制二次贝兹曲线  214.        215.       CGContextSetLineWidth(context, 2.0);  216.    217.       CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);  218.    219.       CGContextMoveToPoint(context, 10, 200);  220.    221.       CGContextAddQuadCurveToPoint(context, 150, 10, 300, 200);  222.        223.       CGContextStrokePath(context);  224.      */   225.         226.     /*绘制虚线  227.     CGContextSetLineWidth(context, 5.0);  228.    229.     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);  230.    231.     CGFloat dashArray[] = {2,6,4,2};  232.    233.     CGContextSetLineDash(context, 3, dashArray, 4);//跳过3个再画虚线,所以刚开始有6-(3-2)=5个虚点  234.        235.     CGContextMoveToPoint(context, 10, 200);  236.        237.     CGContextAddQuadCurveToPoint(context, 150, 10, 300, 200);  238.        239.     CGContextStrokePath(context);  240.     */   241. /*绘制图片  242.     NSString* imagePath = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"];  243.     UIImage* myImageObj = [[UIImage alloc] initWithContentsOfFile:imagePath];  244.     //[myImageObj drawAtPoint:CGPointMake(0, 0)];  245.     [myImageObj drawInRect:CGRectMake(0, 0, 320, 480)];  246.    247.     NSString *s = @"我的小狗";  248.    249.     [s drawAtPoint:CGPointMake(100, 0) withFont:[UIFont systemFontOfSize:34.0]];  250. */   251.         252.   /*  253.     NSString *path = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"];  254.     UIImage *img = [UIImage imageWithContentsOfFile:path];  255.     CGImageRef image = img.CGImage;  256.     CGContextSaveGState(context);  257.     CGRect touchRect = CGRectMake(0, 0, img.size.width, img.size.height);  258.     CGContextDrawImage(context, touchRect, image);  259.     CGContextRestoreGState(context);  260.    */   261.       262.         263.     /*NSString *path = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"];  264.     UIImage *img = [UIImage imageWithContentsOfFile:path];  265.     CGImageRef image = img.CGImage;  266.     CGContextSaveGState(context);  267.    268.     CGContextRotateCTM(context, M_PI);  269.     CGContextTranslateCTM(context, -img.size.width, -img.size.height);  270.    271.     CGRect touchRect = CGRectMake(0, 0, img.size.width, img.size.height);  272.     CGContextDrawImage(context, touchRect, image);  273.     CGContextRestoreGState(context);*/   274.     275. /*  276.     NSString *path = [[NSBundle mainBundle] pathForResource:@"dog" ofType:@"png"];  277.     UIImage *img = [UIImage imageWithContentsOfFile:path];  278.     CGImageRef image = img.CGImage;  279.        280.     CGContextSaveGState(context);  281.    282.     CGAffineTransform myAffine = CGAffineTransformMakeRotation(M_PI);  283.     myAffine = CGAffineTransformTranslate(myAffine, -img.size.width, -img.size.height);  284.     CGContextConcatCTM(context, myAffine);  285.    286.     CGContextRotateCTM(context, M_PI);  287.     CGContextTranslateCTM(context, -img.size.width, -img.size.height);  288.    289.     CGRect touchRect = CGRectMake(0, 0, img.size.width, img.size.height);  290.     CGContextDrawImage(context, touchRect, image);  291.     CGContextRestoreGState(context);  292. */   293. }   0 CGContextRef context = UIGraphicsGetCurrentContext(); 设置上下文 1 CGContextMoveToPoint 开始画线 2 CGContextAddLineToPoint 画直线 4 CGContextAddEllipseInRect 画一椭圆 4 CGContextSetLineCap 设置线条终点形状 4 CGContextSetLineDash 画虚线 4 CGContextAddRect 画一方框 4 CGContextStrokeRect 指定矩形 4 CGContextStrokeRectWithWidth 指定矩形线宽度 4 CGContextStrokeLineSegments 一些直线 5 CGContextAddArc 画已曲线 前俩店为中心 中间俩店为起始弧度 最后一数据为0则顺时针画 1则逆时针 5 CGContextAddArcToPoint(context,0,0, 2, 9, 40);//先画俩条线从point 到 弟1点 , 从弟1点到弟2点的线  切割里面的圆 6 CGContextSetShadowWithColor 设置阴影 7 CGContextSetRGBFillColor 这只填充颜色 7 CGContextSetRGBStrokeColor 画笔颜色设置 7 CGContextSetFillColorSpace 颜色空间填充 7 CGConextSetStrokeColorSpace 颜色空间画笔设置 8 CGContextFillRect 补充当前填充颜色的rect 8 CGContextSetAlaha 透明度 9 CGContextTranslateCTM 改变画布位置 10 CGContextSetLineWidth 设置线的宽度 11 CGContextAddRects 画多个线 12 CGContextAddQuadCurveToPoint 画曲线 13  CGContextStrokePath 开始绘制图片 13 CGContextDrawPath 设置绘制模式 14 CGContextClosePath 封闭当前线路 15 CGContextTranslateCTM(context, 0, rect.size.height);    CGContextScaleCTM(context, 1.0, -1.0);反转画布 16 CGContextSetInterpolationQuality 背景内置颜色质量等级 16 CGImageCreateWithImageInRect 从原图片中取小图 17 字符串的 写入可用  nsstring本身的画图方法 - (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(UILineBreakMode)lineBreakMode alignment:(UITextAlignment)alignment;来写进去即可 18对图片放大缩小的功能就是慢了点      UIGraphicsBeginImageContext(newSize);     UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();   UIGraphicsEndImageContext();   19 CGColorGetComponents() 返回颜色的各个值 以及透明度 可用只读const float 来接收  是个数组 20 画图片 CGImageRef image=CGImageRetain(img.CGImage);      CGContextDrawImage(context, CGRectMake(10.0, height -                    100.0, 90.0, 90.0), image);   21 实现逐变颜色填充方法 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);      22 注:  画完图后,必须      先用CGContextStrokePath来描线,即形状      后用CGContextFillPath来填充形状内的颜色.  填充一个路径的时候,路径里面的子路径都是独立填充的。 假如是重叠的路径,决定一个点是否被填充,有两种规则 1,nonzero winding number rule:非零绕数规则,假如一个点被从左到右跨过,计数器+1,从右到左跨过,计数器-1,最后,如果结果是0,那么不填充,如果是非零,那么填充。 2,even-odd rule: 奇偶规则,假如一个点被跨过,那么+1,最后是奇数,那么要被填充,偶数则不填充,和方向没有关系。  Function Description   CGContextEOFillPath  使用奇偶规则填充当前路径  CGContextFillPath  使用非零绕数规则填充当前路径  CGContextFillRect  填充指定的矩形  CGContextFillRects  填充指定的一些矩形  CGContextFillEllipseInRect  填充指定矩形中的椭圆  CGContextDrawPath  两个参数决定填充规则,kCGPathFill表示用非零绕数规则,kCGPathEOFill表示用奇偶规则,kCGPathFillStroke表示填充,kCGPathEOFillStroke表示描线,不是填充   设置当一个颜色覆盖上另外一个颜色,两个颜色怎么混合 默认方式是 result = (alpha * foreground) + (1 - alpha) * background CGContextSetBlendMode :设置blend mode. CGContextSaveGState :保存blend mode. CGContextRestoreGState:在没有保存之前,用这个函数还原blend mode. CGContextSetBlendMode 混合俩种颜色 说明: ① 绘制图片的底层实现中,原点在左下角 ② 实际开放中尽量不要使用 CGContextDrawImage 和 drawAtPoint 去绘制图片和文字.效率非常低.UILabel,CATextLayer 和 UIImageView 都对低层的绘制有优化,效率比直接绘制高非常多. ③ drawRect: 永远不要手动去调用.如果你需要刷新屏幕把自定义的绘制内容展示在屏幕上,需要调用 UIView 的 setNeedsDisplay 方法去刷新屏幕.调用 setNeedsDisplay 后,系统会在合适的时间去调用 drawRect: 方法刷新屏幕.效率高,节省系统的开销. 保存到相册 UIGraphicsBeginImageContext(currentView.bounds.size); //currentView 当前的view [currentView.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);