iOS?CGContext用法
quartz 是主要的描畫(huà)接口,支持基于路徑的描畫(huà)、抗鋸齒渲染、漸變填充模式、圖像、顏色、坐標(biāo)空間變換、以及PDF 文檔的創(chuàng)建、顯示和分析。UIKit 為Quartz的圖像和顏色操作提供了Objective-C 的封裝。Core Animation為很多UIKit的視圖屬性聲明的動(dòng)畫(huà)效果提供底層支持,也可以用于實(shí)現(xiàn)定制的動(dòng)畫(huà)。
在調(diào)用您提供的drawRect:方法之前,視圖對(duì)象會(huì)自動(dòng)配置其描畫(huà)環(huán)境,使您的代碼可以立即進(jìn)行描畫(huà)。作為這些配置的一部分,UIView 對(duì)象會(huì)為當(dāng)前描畫(huà)環(huán)境創(chuàng)建一個(gè)圖形上下文(對(duì)應(yīng)于CGContextRef 封裝類(lèi)型)
用戶(hù)坐標(biāo)空間是您發(fā)出的所有描畫(huà)命令的工作環(huán)境。該空間的單位由點(diǎn)來(lái)表示。設(shè)備坐標(biāo)空間指的是設(shè)備內(nèi)在的坐標(biāo)空間,由像素來(lái)表示。缺省情況下,用戶(hù)坐標(biāo)空間上的一個(gè)點(diǎn)等于設(shè)備坐標(biāo)空間的一個(gè)像素,這意味著一個(gè)點(diǎn)等于1/160英寸。然而,您不應(yīng)該假定這個(gè)比例總是1:1。
UIColor對(duì)象提供了一些便利方法,用于通過(guò)RGB、HSB、和灰度值指定顏色值。您也可以使用Core Graphics框架中的CGContextSetRGBStrokeColor和CGContextSetRGBFillColor函數(shù)來(lái)創(chuàng)建和設(shè)置顏色。
路徑輪廓可以用像CGContextStrokePath 這樣的函數(shù)來(lái)畫(huà),即用當(dāng)前的筆劃顏色畫(huà)出以路徑為中心位置的線。路徑的填充則可以用CGContextFillPath 函數(shù)來(lái)實(shí)現(xiàn),它的功能是用當(dāng)前的填充顏色或樣式填充路徑線段包圍的區(qū)域。
獲取上下文,圖形上下文是什么意思?
CGContextRef context = UIGraphicsGetCurrentContext();
畫(huà)一個(gè)正方形圖形 沒(méi)有邊框
CGContextSetRGBFillColor(context,0,0.25,0,0.5);
CGContextFillRect(context,CGRectMake(2,2,270,270));
CGContextStrokePath(context);
寫(xiě)文字
CGContextSetLineWidth(context,1.0);
CGContextSetRGBFillColor(context,1,1,1,1.0);
UIFont? *font =[UIFont boldSystemFontOfSize:11.0];
[@"fangyp" drawInRect:CGRectMake(40,40,80,20)withFont:font];
畫(huà)一條線
CGContextSetRGBStrokeColor(context,0.5,0.5,0.5,0.5);//線條顏色
CGContextMoveToPoint(context,20,20);
CGContextAddLineToPoint(context,200,20);
CGContextStrokePath(context);
畫(huà)正方形邊框
CGContextSetRGBStrokeColor(context,1,1.0,1.0,1.0);
CGContextSetLineWidth(context,2.0);
CGContextAddRect(context,CGRectMake(2,2,270,270));
CGContextStrokePath(context);
畫(huà)方形背景顏色
CGContextTranslateCTM(ctx,0.0f,self.view.bounds.size.height);
CGContextScaleCTM(ctx,1.0f,-1.0f);
UIGraphicsPushContext(ctx);
CGContextSetLineWidth(ctx,320);
CGContextSetRGBStrokeColor(ctx,250.0/255,250.0/255,210.0/255,1.0);
CGContextStrokeRect(ctx,CGRectMake(0,0,320,460));
UIGraphicsPopContext();
1、畫(huà)線:在uiview類(lèi)里重寫(xiě)下面方法
-(void)drawRect:(CGRect)rect
{
CGContextRefcontext = UIGraphicsGetCurrentContext();
//畫(huà)線
//??? UIColor*aColor =[UIColor colorWithRed:0 green:1.0 blue:0 alpha:0];
CGContextSetRGBStrokeColor(context,1.0,0,0,1.0);
//???CGContextSetFillColorWithColor(context,aColor.CGColor);
CGContextSetLineWidth(context,4.0);
CGPointaPoints[5];
aPoints[0]=CGPointMake(60,60);
aPoints[1]=CGPointMake(260,60);
aPoints[2]=CGPointMake(260,300);
aPoints[3]=CGPointMake(60,300);
aPoints[4]=CGPointMake(60,60);
CGContextAddLines(context,aPoints,5);
CGContextDrawPath(context,kCGPathStroke);//開(kāi)始畫(huà)線
//橢圓
CGRect aRect= CGRectMake(80,80,160,100);
CGContextSetRGBStrokeColor(context,0.6,0.9,0,1.0);
CGContextSetLineWidth(context,3.0);
//???CGContextSetFillColorWithColor(context,aColor.CGColor);
//???CGContextAddRect(context,rect);//矩形
CGContextAddEllipseInRect(context,aRect);//橢圓
CGContextDrawPath(context,kCGPathStroke);
2、弧線CGContextAddArcToPoint與CGContextAddArc
void?CGContextAddArc(CGContextRef?c,CGFloat?x,CGFloat?y,CGFloat?radius,CGFloat?startAngle,CGFloat?endAngle,int?clockwise)
x,y為圓點(diǎn)坐標(biāo),startAngle為開(kāi)始的弧度,endAngle為結(jié)束的弧度,clockwise 0為順時(shí)針,1為逆時(shí)針。
以下是示例代碼。
CGContextBeginPath(context);
CGContextSetRGBStrokeColor(context,0,1,0,1);
CGContextAddArc(context,100,100,50,180* PI/ 180,270* PI/ 180,0);
CGContextStrokePath(context);
void?CGContextAddArcToPoint(CGContextRef?c,CGFloat?x1,CGFloat?y1,CGFloat?x2,CGFloat?y2,CGFloat?radius);
首先使用該函數(shù)繪制圓弧前,首先要確定一個(gè)start point.
CGContextMoveToPoint(context,100,100);
然后設(shè)置CGContextAddArcToPoint(context,50,100,50,150,50);
這里是從起始點(diǎn)100,100開(kāi)始到第一個(gè)點(diǎn)50,100畫(huà)一條線段,然后再?gòu)牡谝粋€(gè)點(diǎn)50,100到第二點(diǎn)150,50畫(huà)另一條線段(這是兩條相交切線),然后設(shè)置半徑為50.通過(guò)相交的兩條線段和半徑就可以確定圓弧了。
示例代碼如下:
CGContextBeginPath(context);
CGContextSetRGBStrokeColor(context,0,0,1,1);
CGContextMoveToPoint(context,100,100);
CGContextAddArcToPoint(context,50,100,50,150,50);
CGContextStrokePath(context);
注意:Path被繪制后,當(dāng)前點(diǎn)的坐標(biāo)更改為150,50
CGContextRef context = UIGraphicsGetCurrentContext();設(shè)置上下文
CGContextMoveToPoint開(kāi)始畫(huà)線
CGContextAddLineToPoint畫(huà)直線
4 CGContextAddEllipseInRect畫(huà)一橢圓
4 CGContextSetLineCap設(shè)置線條終點(diǎn)形狀
4 CGContextSetLineDash畫(huà)虛線
4 CGContextAddRect畫(huà)一方框
4 CGContextStrokeRect指定矩形
4 CGContextStrokeRectWithWidth指定矩形線寬度
4 CGContextStrokeLineSegments一些直線
5 CGContextAddArc 畫(huà)已曲線 前倆店為中心 中間倆店為起始弧度 最后一數(shù)據(jù)為0則順時(shí)針畫(huà) 1則逆時(shí)針
5 CGContextAddArcToPoint(context,0,0,2,9,40);//先畫(huà)倆條線從point到弟1點(diǎn),從弟1點(diǎn)到弟2點(diǎn)的線切割里面的圓
6 CGContextSetShadowWithColor設(shè)置陰影
7 CGContextSetRGBFillColor這只填充顏色
7 CGContextSetRGBStrokeColor畫(huà)筆顏色設(shè)置
7 CGContextSetFillColorSpace顏色空間填充
7 CGConextSetStrokeColorSpace顏色空間畫(huà)筆設(shè)置
8 CGContextFillRect補(bǔ)充當(dāng)前填充顏色的rect
8 CGContextSetAlaha透明度
9 CGContextTranslateCTM改變畫(huà)布位置
10 CGContextSetLineWidth設(shè)置線的寬度
11 CGContextAddRects畫(huà)多個(gè)線
12 CGContextAddQuadCurveToPoint畫(huà)曲線
13 ?CGContextStrokePath開(kāi)始繪制圖片
13 CGContextDrawPath設(shè)置繪制模式
14 CGContextClosePath封閉當(dāng)前線路
15 CGContextTranslateCTM(context,0,rect.size.height);CGContextScaleCTM(context,1.0,-1.0);反轉(zhuǎn)畫(huà)布
16 CGContextSetInterpolationQuality背景內(nèi)置顏色質(zhì)量等級(jí)
16 CGImageCreateWithImageInRect從原圖片中取小圖
17字符串的寫(xiě)入可用nsstring本身的畫(huà)圖方法-(CGSize)drawInRect:(CGRect)rect withFont:(UIFont *)font lineBreakMode:(UILineBreakMode)lineBreakMode alignment:(UITextAlignment)alignment;來(lái)寫(xiě)進(jìn)去即可
18對(duì)圖片放大縮小的功能就是慢了點(diǎn)?
UIGraphicsBeginImageContext(newSize);
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
19 CGColorGetComponents()返回顏色的各個(gè)直以及透明度可用只讀const float來(lái)接收是個(gè)數(shù)組
20畫(huà)圖片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 注: ?畫(huà)完圖后,必須?
先用CGContextStrokePath來(lái)描線,即形狀
后用CGContextFillPath來(lái)填充形狀內(nèi)的顏色.
填充一個(gè)路徑的時(shí)候,路徑里面的子路徑都是獨(dú)立填充的。
假如是重疊的路徑,決定一個(gè)點(diǎn)是否被填充,有兩種規(guī)則
1,nonzero winding number rule:非零繞數(shù)規(guī)則,假如一個(gè)點(diǎn)被從左到右跨過(guò),計(jì)數(shù)器+1,從右到左跨過(guò),計(jì)數(shù)器-1,最后,如果結(jié)果是0,那么不填充,如果是非零,那么填充。
2,even-odd rule: 奇偶規(guī)則,假如一個(gè)點(diǎn)被跨過(guò),那么+1,最后是奇數(shù),那么要被填充,偶數(shù)則不填充,和方向沒(méi)有關(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:在沒(méi)有保存之前,用這個(gè)函數(shù)還原blend mode.
CGContextSetBlendMode混合倆種顏色