IOS開發之——繪圖(CGContext)

來源:互聯網
上載者:User

標籤:des   blog   http   color   io   os   使用   ar   strong   

CSDN部落格原文  http://blog.csdn.net/zhenyu5211314/article/details/24230581

0    CGContextRef context = UIGraphicsGetCurrentCont ext(); 設定上下文 
1 CGContextMoveToPoint 開始畫線 
2 CGContextAddLineToPoint 畫直線 
4 CGContextAddEllipseInRec t 畫一橢圓 
4 CGContextSetLineCap 設定線條終點形狀 
4 CGContextSetLineDash 畫虛線 
4 CGContextAddRect 畫一方框 
4 CGContextStrokeRect 指定矩形 
4 CGContextStrokeRectWithW idth 指定矩形線寬度 
4 CGContextStrokeLineSegme nts 一些直線 
5 CGContextAddArc 畫已曲線 前倆店為中心 中間倆店為起始弧度 最後一資料為0則順時針畫 1則逆時針 
5 CGContextAddArcToPoint(context,0,0, 2, 9, 40);//先畫倆條線從point 到 弟1點 , 從弟1點到弟2點的線    切割裡面的圓 
6 CGContextSetShadowWithCo lor 設定陰影 
7 CGContextSetRGBFillColor  這隻填充 顏色 
7 CGContextSetRGBStrokeCol or 畫筆顏色設定 
7 CGContextSetFillColorSpa ce 色彩空間填充 
7 CGConextSetStrokeColorSp ace 色彩空間畫筆設定 
8 CGContextFillRect 補充當前填充顏色的rect 
8 CGContextSetAlaha 透明度 
9 CGContextTranslateCTM 改變畫布位置 
10 CGContextSetLineWidth 設定線的寬度 
11 CGContextAddRects 畫多個線 
12 CGContextAddQuadCurveToP oint 畫曲線 
13    CGContextStrokePath 開始繪製 圖片 
13 CGContextDrawPath 設定繪製模式 
14 CGContextClosePath 封閉當前線路 
15 CGContextTranslateCTM(context, 0, rect.size.height);        CGContextScaleCTM(context, 1.0, -1.0);反轉畫布 
16 CGContextSetInterpolatio nQuality 背景內建顏色品質等級 
16 CGImageCreateWithImageIn Rect 從原圖片中取小圖 
17  字串 的 寫入可用    nsstring本身的畫圖方法 - (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(UILineBreakMode)lineBreakMode alignment:(UITextAlignment)alignment;來寫進去即可 
18對圖片放大縮小的功能就是慢了點 
       UIGraphicsBeginImageCont ext(newSize); 
        UIImage* newImage = UIGraphicsGetImageFromCu rrentImageContext(); 
    UIGraphicsEndImageContex t(); 
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 = CGColorSpaceCreateDevice RGB(); 
        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 = CGGradientCreateWithColo rComponents           
     (rgb, colors, NULL, sizeof(colors)/(sizeof(colors[0])*4)); 
        CGColorSpaceRelease(rgb);        
        CGContextDrawLinearGradi ent(context, gradient,CGPointMake        
     (0.0,0.0) ,CGPointMake(0.0,self.frame.size.height),                                        
         kCGGradientDrawsBeforeSt artLocation); 
        
22 注:   畫完圖後,必須 
        先用CGContextStrokePath來描線,即形狀 
        後用CGContextFillPath來填充形狀內的顏色. 
填充一個路徑的時候,路徑裡面的子路徑都是獨立填充的。 
假如是重疊的路徑,決定一個點是否被填充,有兩種規則 
1,nonzero winding number rule:非零繞數規則,假如一個點被從左至右跨過,計數器+1,從右至左跨過,計數器-1,最後,如果結果是0,那麼不填充,如果是非零,那麼填充。 
2,even-odd rule: 奇偶規則,假如一個點被跨過,那麼+1,最後是奇數,那麼要被填充,偶數則不填充,和方向沒有關係。 
  Function 
Description  
  CGContextEOFillPath 
  使用奇偶規則填充當前路徑 
  CGContextFillPath 
  使用非零繞數規則填充當前路徑 
  CGContextFillRect 
  填充指定的矩形 
  CGContextFillRects 
  填充指定的一些矩形 
  CGContextFillEllipseInRe ct 
  填充指定矩形中的橢圓 
  CGContextDrawPath 
  兩個 參數 決定填充規則,kCGPathFill表示用非零繞數規則,kCGPathEOFill表示用奇偶規則,kCGPathFillStroke表示填充,kCGPathEOFillStroke表示描線,不是填充 
設定當一個顏色覆蓋上另外一個顏色,兩個顏色怎麼混合 
預設是 
result = (alpha * foreground) + (1 - alpha) * background 
CGContextSetBlendMode :設定blend mode. 
CGContextSaveGState :儲存blend mode. 
CGContextRestoreGState:在沒有儲存之前,用這個函數還原blend mode. 
CGContextSetBlendMode 混合倆種顏色

http://www.cocoachina.com/bbs/read.php?tid=75122&page=1

================================================================ 

虛線

畫虛線需要用到函數:

CGContextSetLineDash

此函數需要四個參數:

  • context   – 這個不用多說
  • phase   - 稍後再說
  • lengths   – 指明虛線是如何交替繪製,具體看例子
  • count  –  lengths數組的長度
  1. CGContextRef context =UIGraphicsGetCurrentContext();  
  2. CGContextBeginPath(context);  
  3. CGContextSetLineWidth(context, 2.0);  
  4. CGContextSetStrokeColorWithColor(context, [UIColorwhiteColor].CGColor);  
  5. float  lengths[] = {10,10};  
  6. CGContextSetLineDash(context, 0, lengths,2);  
  7. CGContextMoveToPoint(context, 10.0, 20.0);  
  8. CGContextAddLineToPoint(context, 310.0,20.0);  
  9. CGContextStrokePath(context);  
  10. CGContextClosePath(context);  

lengths的值{10,10}表示先繪製10個點,再跳過10個點,如此反覆,

如果把lengths值改為 {10, 20, 10},則表示先繪製10個點,跳過20個點,繪製10個點,跳過10個點,再繪製20個點,如此反覆,

注意count的值等於lengths數組的長度

phase參數表示在第一個虛線繪製的時候跳過多少個點,舉例說明:

  1. float  lengths[] = {10,5};  
  2. CGContextSetLineDash(context, 0, lengths, 2);    
  3. CGContextMoveToPoint(context, 0.0, 20.0);    
  4. CGContextAddLineToPoint(context, 310.0, 20.0);     
  5. CGContextStrokePath(context);  
  6.                           
  7. CGContextSetLineDash(context, 5, lengths, 2);  
  8. CGContextMoveToPoint(context, 0.0, 40.0);    
  9. CGContextAddLineToPoint(context, 310.0, 40.0);  
  10. CGContextStrokePath(context);             
  11.                                               
  12. CGContextSetLineDash(context, 8, lengths, 2);     
  13. CGContextMoveToPoint(context, 0.0, 60.0);             
  14. CGContextAddLineToPoint(context, 310.0, 60.);             
  15. CGContextStrokePath(context);   
顯示:

由於lengths值為{10,5},第一條線就是繪製10,跳過5,反覆繪製。

第二條線的phase值為5,則首先繪製【10減去5】,再跳過5,繪製10,反覆繪製。

第三條給也如此,先繪製2,再跳過5,如此反覆。

CGContextSetStrokeColorW ithColor(myContext, [UIColor blackColor].CGColor); 
CGContextSetLineDash (myContext,phase,lengths,2); 
CGContextClosePath(myContext); 
CGContextStrokePath(myContext);

================================================================

切線

- (void)drawRect:(CGRect)rect {

        CGContextRef context = UIGraphicsGetCurrentContext();

        CGContextSetLineWidth(context, 2.0);

        CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);

               CGContextMoveToPoint(context, 100, 100); 
               CGContextAddArcToPoint(context, 100,200, 300,200, 100); 
               CGContextStrokePath(context); 
}

================================================================

- (void)drawRect:(CGRect)rect {

        CGContextRef context = UIGraphicsGetCurrentContext();

        CGContextSetLineWidth(context, 2.0);

        CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);

        CGRect rectangle = CGRectMake(60,170,200,80);

        CGContextAddEllipseInRect(context, rectangle);

               CGContextStrokePath(context); 
}

================================================================

- (void)drawRect:(CGRect)rect {

        CGContextRef context = UIGraphicsGetCurrentContext();

        CGContextSetLineWidth(context, 2.0);

        CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);

        CGContextMoveToPoint(context, 10, 10);

        CGContextAddCurveToPoint(context, 0, 50, 300, 250, 300, 400);

               CGContextStrokePath(context); 
}

================================================================

- (void)drawRect:(CGRect)rect {

        CGContextRef context = UIGraphicsGetCurrentContext();

        CGContextSetLineWidth(context, 2.0);

        CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);

        CGContextMoveToPoint(context, 10, 200);

        CGContextAddQuadCurveToPoint(context, 150, 10, 300, 200);

               CGContextStrokePath(context); 
}

================================================================

- (void)drawRect:(CGRect)rect {

        CGContextRef context = UIGraphicsGetCurrentContext();

        CGContextSetLineWidth(context, 5.0);

        CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);

        CGFloat dashArray[] = {2,6,4,2};

        CGContextSetLineDash(context, 3, dashArray, 4);

        CGContextMoveToPoint(context, 10, 200);

        CGContextAddQuadCurveToPoint(context, 150, 10, 300, 200);

               CGContextStrokePath(context); 
}

IOS開發之——繪圖(CGContext)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.