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

  1. - (void)drawRect:(CGRect)rect  
  2. {  
  3.     CGContextRef context = UIGraphicsGetCurrentContext();  

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

猜你喜欢

转载自blog.csdn.net/u012556024/article/details/40707813