Quartz 2D是一個二維繪圖引擎。Quartz 2D的API是C語言,來自于CoreGraphics框架。沒有面相對象的思想。
1.作用:
繪制圖形;線條,三Quartz 2D角形,矩形,圓,圓弧等。
繪制文字
繪制,生成圖片,圖像
讀取,生成PDF
截圖,裁減圖片
自定義UI控件
2.圖形上下文(Graphics Context):是一個CGContextRef類型的數據。
圖形上下文的作用:A.保存繪圖信息,狀態;
B.決定繪制的輸出目標(繪制到什么地方去,輸出目標可以是PDF文件。Bitmap或者顯示器的窗口上)
在UIView中繪圖只能在DrawRect方法中獲得圖形上下文,并繪圖。drawRect系統調用,不能手動調用。視圖顯示在屏幕上的時候調用,且只調用一次。
現在來看下代碼。
首先新建工程,在Stroyboard上建一個view,這個view待會就是我們的畫板
繪制直線,四邊形,三角形
// 系統自動調用
- (void)drawRect:(CGRect)rect {
// Drawing code
[self drawLine];
//[self drawR];
//[self drawTriangle];
}
//畫線
- (void) drawLine
{
//1.獲得圖形上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//將上下文復制一份到棧中
CGContextSaveGState(context);
//2.繪制圖形
//第一條線
//設置線段寬度
CGContextSetLineWidth(context, 20);
//設置線條頭尾部的樣式
CGContextSetLineCap(context, kCGLineCapRound);
//設置顏色
CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);
//設置起點
CGContextMoveToPoint(context, 10, 10);
//畫線
CGContextAddLineToPoint(context, 100, 100);
//3.顯示到View
CGContextStrokePath(context);//以空心的方式畫出
//將圖形上下文出棧,替換當前的上下文
CGContextRestoreGState(context);
[[UIColor blueColor] set];
//設置線段轉折點的樣式
CGContextSetLineJoin(context, kCGLineJoinRound);
//畫線
CGContextMoveToPoint(context, 100, 120);
CGContextAddLineToPoint(context, 150, 120);
CGContextAddLineToPoint(context, 150, 180);
//3.顯示到view
CGContextStrokePath(context);
}
//繪制四邊形
- (void) drawR
{
//1.獲得圖形上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//2.繪制四邊形
CGContextAddRect(context, CGRectMake(20, 20, 100, 100));
//設置顏色
[[UIColor purpleColor] setFill];
//3.顯示在view上
CGContextFillPath(context);
}
//繪制三角形
- (void) drawTriangle
{
//1.獲得圖形上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//繪制三角形
CGContextMoveToPoint(context, 50, 0);
CGContextAddLineToPoint(context, 100, 100);
CGContextAddLineToPoint(context, 0, 100);
//關閉路徑,閉環,(連接起點和最后一個點)
CGContextSetLineWidth(context, 2);
CGContextClosePath(context);
[[UIColor whiteColor] set];
//顯示在view上
CGContextStrokePath(context);
}
繪制圓,圓弧,貝塞爾曲線,文字,圖片
- (void)drawRect:(CGRect)rect {
// Drawing code
//drawCircle();
drawArc();
//drawText();
//drawImg();
//drawBezier();
}
//畫圓
void drawCircle()
{
//1.獲取圖形上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//2.繪制圖形
CGContextAddEllipseInRect(context, CGRectMake(50, 50, 100, 100));
CGContextSetLineWidth(context, 2);
//3.顯示在View上
CGContextStrokePath(context);
}
//畫圓弧
void drawArc()
{
//1.獲得圖形上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//2.繪制圖形
CGContextAddArc(context, 100, 100, 50, arc(90), arc(30), 0);
//CGContextAddArc(context, 100, 100, 50, M_PI_2, M_PI, 0);
//? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 90,? ? 180, 1逆時針,0順時針
//3.顯示
CGContextStrokePath(context);
}
//繪制文字
void drawText()
{
NSString *str = @"測試文本";
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
attributes[NSFontAttributeName] = [UIFont systemFontOfSize:20];//設置文字大小
attributes[NSForegroundColorAttributeName] = [UIColor purpleColor];
[str drawInRect:CGRectMake(100, 100, 100, 30) withAttributes:attributes];
}
//畫圖片
void drawImg()
{
//1.取得圖片
UIImage *img = [UIImage imageNamed:@"120"];
//2.畫
//[img drawAtPoint:CGPointMake(20, 20)];//在(20,20)這個位置開始畫
//[img drawInRect:CGRectMake(20, 20, 100, 100)];//設置起始點,和寬高,會自動拉伸圖片
[img drawAsPatternInRect:CGRectMake(0, 0, 190, 190)];//不會自動拉伸圖片,不夠時會自動平鋪,類似于格子
//增加文字,也可以設置水印
NSString *str = @"測試文本";
[str drawInRect:CGRectMake(0, 0, 100, 30) withAttributes:nil];
}
//貝塞爾曲線
void drawBezier()
{
//1.取得圖形上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//起點
CGContextMoveToPoint(context, 10, 10);
//2個控制點
//CGContextAddCurveToPoint(context, 120, 100,? 180, 50,? 10, 190);
//? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 第一個控制點,第二個控制點,? 終點
//1個控制點
CGContextAddQuadCurveToPoint(context, 150, 200, 200, 100);
CGContextStrokePath(context);
}