
Core Graphics
Core Graphics Framework是一套基于C的API框架,使用了Quartz作為繪圖引擎。它提供了低級(jí)別、輕量級(jí)、高保真度的2D渲染。該框架可以用于基于路徑的繪圖、變換、顏色管理、脫屏渲染,模板、漸變、遮蔽、圖像數(shù)據(jù)管理、圖像的創(chuàng)建、遮罩以及PDF文檔的創(chuàng)建、顯示和分析。
Core Graphics API所有的操作都在一個(gè)上下文中進(jìn)行
所以在繪圖之前需要獲取該上下文并傳入執(zhí)行渲染的函數(shù)中。如果你正在渲染一副在內(nèi)存中的圖片,此時(shí)就需要傳入圖片所屬的上下文。獲得一個(gè)圖形上下文是我們完成繪圖任務(wù)的第一步,你可以將圖形上下文理解為一塊畫(huà)布。如果你沒(méi)有得到這塊畫(huà)布,那么你就無(wú)法完成任何繪圖操作。當(dāng)然,有許多方式獲得一個(gè)圖形上下文,這里我介紹兩種最為常用的獲取方法。創(chuàng)建一個(gè)圖片類型的上下文
調(diào)用UIGraphicsBeginImageContextWithOptions函數(shù)就可獲得用來(lái)處理圖片的圖形上下文。利用該上下文,你就可以在其上進(jìn)行繪圖,并生成圖片。調(diào)用UIGraphicsGetImageFromCurrentImageContext函數(shù)可從當(dāng)前上下文中獲取一個(gè)UIImage對(duì)象。記住在你所有的繪圖操作后別忘了調(diào)用UIGraphicsEndImageContext函數(shù)關(guān)閉圖形上下文。利用cocoa為你生成的圖形上下文
當(dāng)你子類化了一個(gè)UIView并實(shí)現(xiàn)了自己的drawRect:方法后,一旦drawRect:方法被調(diào)用,Cocoa就會(huì)為你創(chuàng)建一個(gè)圖形上下文,此時(shí)你對(duì)圖形上下文的所有繪圖操作都會(huì)顯示在UIView上。drawRect方法什么時(shí)候觸發(fā)
1.當(dāng)view第一次顯示到屏幕上時(shí);
2.當(dāng)調(diào)用view的setNeedsDisplay或者setNeedsDisplayInRect:方法時(shí)。
步驟
- 先在drawRect方法中獲得上下文context;
- 繪制圖形(線,圖形,圖片等);
- 設(shè)置一些修飾屬性;
- 渲染到上下文,完成繪圖。
實(shí)例
直線
- (void)drawRect:(CGRect)rect
{
CGContextRef ref = UIGraphicsGetCurrentContext();// 獲取上下文
CGContextMoveToPoint(ref, 20, 40); // 起點(diǎn)
CGContextAddLineToPoint(ref, WIDTH(self)-20, 40); //終點(diǎn)
// CGContextSetRGBStrokeColor(ref, 0, 1.0, 0, 1.0); // 顏色
[[UIColor redColor] set]; // 兩種設(shè)置顏色的方式都可以
CGContextSetLineWidth(ref, 2.0f); // 線的寬度
CGContextSetLineCap(ref, kCGLineCapRound); // 起點(diǎn)和終點(diǎn)圓角
CGContextSetLineJoin(ref, kCGLineJoinRound); // 轉(zhuǎn)角圓角
CGContextStrokePath(ref); // 渲染(直線只能繪制空心的,不能調(diào)用CGContextFillPath(ref);)
}
曲線
- (void)drawRect:(CGRect)rect {
// Drawing code
CGContextRef ref = UIGraphicsGetCurrentContext();
CGContextBeginPath(ref);
// 起點(diǎn)
CGContextMoveToPoint(ref, 100, 70);
/*
cpx cpy 控制點(diǎn)
x y 終點(diǎn)
*/
CGContextAddQuadCurveToPoint(ref, 150, 100, 200, 40);
CGContextSetLineWidth(ref, 5);
CGContextSetStrokeColorWithColor(ref, [UIColor brownColor].CGColor);
CGContextStrokePath(ref);
}
虛線
- (void)drawRect:(CGRect)rect {
// Drawing code
CGContextRef ref = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(ref, 5.0);
CGContextSetStrokeColorWithColor(ref, [UIColor blueColor].CGColor);
CGFloat dashArray[] = {2,6,4,2};
/*
context – 這個(gè)不用多說(shuō)
phase - phase參數(shù)表示在第一個(gè)虛線繪制的時(shí)候跳過(guò)多少個(gè)點(diǎn)
lengths – lengths的值{2,6,4,2}表示先繪制2個(gè)點(diǎn),再跳過(guò)6個(gè)點(diǎn),繪制4個(gè)點(diǎn),跳過(guò)2個(gè)點(diǎn),繪制2個(gè)點(diǎn),以此往復(fù)
count – 注意count的值等于lengths數(shù)組的長(zhǎng)度
*/
CGContextSetLineDash(ref, 3, dashArray, 4);
CGContextMoveToPoint(ref, 20, 40); // 起點(diǎn)
CGContextAddLineToPoint(ref, WIDTH(self)-20, 40); //終點(diǎn)
CGContextStrokePath(ref);
}
橢圓
- (void)drawRect:(CGRect)rect {
// Drawing code
CGContextRef ref = UIGraphicsGetCurrentContext();
/*
rect 坐標(biāo)位置 長(zhǎng)寬相等即為圓
*/
CGContextAddEllipseInRect(ref, CGRectMake((WIDTH(self) - 60)/2, (HEIGHT(self) - 40)/2, 60, 40));
[[UIColor redColor] set];
CGContextFillPath(ref);
}
圓
- (void)drawRect:(CGRect)rect
{
CGContextRef ref = UIGraphicsGetCurrentContext();
// 畫(huà)圓
CGContextAddEllipseInRect(ref, CGRectMake((WIDTH(self) - HEIGHT(self))/2, 0, HEIGHT(self), HEIGHT(self)));
// 著色
[[UIColor greenColor] set];
/*
context表示要作畫(huà)的內(nèi)容
offset表示陰影的位置
blur表示陰影的模糊度
color表示圖形要填充的顏色
*/
CGContextSetShadowWithColor(ref, CGSizeMake(10, 0), 20.0f, [[UIColor redColor] CGColor]);
// 渲染
CGContextFillPath(ref);
// 添加空心
CGContextAddEllipseInRect(ref, CGRectMake((WIDTH(self)-30)/2, (HEIGHT(self)-30)/2, 30, 30));
[[UIColor whiteColor] set];
CGContextFillPath(ref);
}
弧線
- (void)drawRect:(CGRect)rect {
// Drawing code
CGContextRef ref = UIGraphicsGetCurrentContext();
//畫(huà)筆線的顏色
// CGContextSetRGBStrokeColor(ref, 0.30, 0.62, 0.30, 1);
CGContextSetStrokeColorWithColor(ref, [UIColor blueColor].CGColor);
//線的寬度
CGContextSetLineWidth(ref, 5.0);
/*
1弧度=180°/π (≈57.3°)
度=弧度×180°/π
360°= 360×π/180 =2π 弧度
然后:
x,y為圓點(diǎn)坐標(biāo),radius半徑,startAngle為開(kāi)始的弧度,endAngle為 結(jié)束的弧度,clockwise 0為順時(shí)針,1為逆時(shí)針。
*/
CGContextAddArc(ref, self.center.x, 40, 35, 0, 2 * M_PI, 0); //中心y坐標(biāo)不能是self.center.y why ?
//繪制路徑
CGContextDrawPath(ref, kCGPathStroke);
}
看看效果
