CGContext用法詳解_轉(zhuǎn)

原文鏈接如下:

http://blog.csdn.net/zhenyu5211314/article/details/24230581?utm_source=tuicool&utm_medium=referral

0? CGContextRef context = UIGraphicsGetCurrentContext(); 設(shè)置上下文

1 CGContextMoveToPoint 開始畫線

2 CGContextAddLineToPoint 畫直線

4 CGContextAddEllipseInRect 畫一橢圓

4 CGContextSetLineCap 設(shè)置線條終點(diǎn)形狀

4 CGContextSetLineDash 畫虛線

4 CGContextAddRect 畫一方框

4 CGContextStrokeRect 指定矩形

4 CGContextStrokeRectWithWidth 指定矩形線寬度

4 CGContextStrokeLineSegments 一些直線

5 CGContextAddArc 畫已曲線 前倆店為中心 中間倆店為起始弧度 最后一數(shù)據(jù)為0則順時(shí)針畫 1則逆時(shí)針

5 CGContextAddArcToPoint(context,0,0, 2, 9, 40);//先畫倆條線從point 到 弟1點(diǎn) , 從弟1點(diǎn)到弟2點(diǎn)的線? 切割里面的圓

6 CGContextSetShadowWithColor 設(shè)置陰影

7 CGContextSetRGBFillColor 這只填充顏色

7 CGContextSetRGBStrokeColor 畫筆顏色設(shè)置

7 CGContextSetFillColorSpace 顏色空間填充

7 CGConextSetStrokeColorSpace 顏色空間畫筆設(shè)置

8 CGContextFillRect 補(bǔ)充當(dāng)前填充顏色的rect

8 CGContextSetAlaha 透明度

9 CGContextTranslateCTM 改變畫布位置

10 CGContextSetLineWidth 設(shè)置線的寬度

11 CGContextAddRects 畫多個(gè)線

12 CGContextAddQuadCurveToPoint 畫曲線

13? CGContextStrokePath 開始繪制圖片

13 CGContextDrawPath 設(shè)置繪制模式

14 CGContextClosePath 封閉當(dāng)前線路

15 CGContextTranslateCTM(context, 0, rect.size.height);? ? CGContextScaleCTM(context, 1.0, -1.0);反轉(zhuǎn)畫布

16 CGContextSetInterpolationQuality 背景內(nèi)置顏色質(zhì)量等級

16 CGImageCreateWithImageInRect 從原圖片中取小圖

17 字符串的 寫入可用? nsstring本身的畫圖方法 - (CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(UILineBreakMode)lineBreakMode alignment:(UITextAlignment)alignment;來寫進(jìn)去即可

18對圖片放大縮小的功能就是慢了點(diǎn)

UIGraphicsBeginImageContext(newSize);

UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

19 CGColorGetComponents() 返回顏色的各個(gè)直 以及透明度 可用只讀const float 來接收? 是個(gè)數(shù)組

20 畫圖片 CGImageRef image=CGImageRetain(img.CGImage);

CGContextDrawImage(context, CGRectMake(10.0, height -

100.0, 90.0, 90.0), image);

21 實(shí)現(xiàn)逐變顏色填充方法 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來填充形狀內(nèi)的顏色.

填充一個(gè)路徑的時(shí)候,路徑里面的子路徑都是獨(dú)立填充的。

假如是重疊的路徑,決定一個(gè)點(diǎn)是否被填充,有兩種規(guī)則

1,nonzero winding number rule:非零繞數(shù)規(guī)則,假如一個(gè)點(diǎn)被從左到右跨過,計(jì)數(shù)器+1,從右到左跨過,計(jì)數(shù)器-1,最后,如果結(jié)果是0,那么不填充,如果是非零,那么填充。

2,even-odd rule: 奇偶規(guī)則,假如一個(gè)點(diǎn)被跨過,那么+1,最后是奇數(shù),那么要被填充,偶數(shù)則不填充,和方向沒有關(guān)系。

Function

Description

CGContextEOFillPath

使用奇偶規(guī)則填充當(dāng)前路徑

CGContextFillPath

使用非零繞數(shù)規(guī)則填充當(dāng)前路徑

CGContextFillRect

填充指定的矩形

CGContextFillRects

填充指定的一些矩形

CGContextFillEllipseInRect

填充指定矩形中的橢圓

CGContextDrawPath

兩個(gè)參數(shù)決定填充規(guī)則,kCGPathFill表示用非零繞數(shù)規(guī)則,kCGPathEOFill表示用奇偶規(guī)則,kCGPathFillStroke表示填充,kCGPathEOFillStroke表示描線,不是填充

設(shè)置當(dāng)一個(gè)顏色覆蓋上另外一個(gè)顏色,兩個(gè)顏色怎么混合

默認(rèn)方式是

result = (alpha * foreground) + (1 - alpha) * background

CGContextSetBlendMode :設(shè)置blend mode.

CGContextSaveGState :保存blend mode.

CGContextRestoreGState:在沒有保存之前,用這個(gè)函數(shù)還原blend mode.

CGContextSetBlendMode 混合倆種顏色

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容