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

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

猜你喜欢

转载自blog.csdn.net/sevenquan/article/details/66973990