Core Graphics(CG)是 iOS 的底層 2D 繪圖引擎。凡是客製圖形、進度環、漸層背景、圖表,都可以在 drawRect: 中用它畫出來。所有繪製都圍繞一個 CGContextRef 上下文展開,取得上下文是一切的第一步。
基本流程固定:取得上下文 → 建立路徑或設定參數 → 設定顏色與線寬 → 描邊或填充 → 釋放資源。以下逐一示範。
_context = UIGraphicsGetCurrentContext();
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 50, 50);
CGPathAddLineToPoint(path, NULL, 150, 150);
CGPathAddLineToPoint(path, NULL, 50, 150);
CGPathCloseSubpath(path);
CGContextAddPath(_context, path);
CGFloat myColor[4] = {1.0f, 1.0f, 0, 1.0f};
CGContextSetFillColor(_context, myColor);
CGContextSetStrokeColor(_context, myColor);
CGContextSetShouldAntialias(_context, NO);
CGContextSetLineWidth(_context, 15.0);
CGContextSetLineJoin(_context, kCGLineJoinRound);
CGContextDrawPath(_context, kCGPathStroke);
CGPathRelease(path);
CGContextRelease(_context);
CGRect rect2 = CGRectMake(0, 0, 200, 200);
CGContextSetStrokeColorWithColor(_context, [UIColor redColor].CGColor);
CGContextSetLineWidth(_context, 2.0);
CGContextStrokeRect(_context, rect2);
漸層的關鍵是「裁剪區域」:先畫出範圍並 CGContextClip,再畫漸層,顏色才不會溢出。
_context = UIGraphicsGetCurrentContext();
// 色彩空間
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// 漸層色值
NSArray *colors = @[(__bridge id)[UIColor redColor].CGColor,
(__bridge id)[UIColor greenColor].CGColor];
// 各色停靠位置
CGFloat location[2] = {0, 1};
// 建立漸層對象
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace,
(__bridge CFArrayRef)colors,
location);
CGContextSaveGState(_context);
CGRect rect2 = CGRectMake(0, 0, 150, 150);
CGContextAddRect(_context, rect2); // 繪製區域
CGContextClip(_context); // 裁剪
CGContextDrawLinearGradient(_context, gradient,
CGPointMake(0, 0),
CGPointMake(150, 150),
kCGGradientDrawsAfterEndLocation);
CGContextRestoreGState(_context);
CGColorSpaceRelease(colorSpace);
CGGradientRelease(gradient);
CGContextRelease(_context);
UIFont *font = [UIFont fontWithName:@"Gill Sans" size:30];
NSString *drawText = @"hello !";
[drawText drawAtPoint:CGPointMake(30, 100) withFont:font];
UIImage *image = [UIImage imageNamed:@"頭像2.png"];
[image drawAtPoint:CGPointMake(30, 30)];
CGImageRelease([image CGImage]);
實際專案中,多數情況會用 UIBezierPath 取代裸 CGPath,它內建了畫線、畫圓、畫矩形、畫橢圓的便利構造器,並自動管理內存。
// 折線
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(30, 30)];
[path addLineToPoint:CGPointMake(30, 120)];
[path addLineToPoint:CGPointMake(120, 120)];
[path closePath];
// 圓弧/圓
UIBezierPath *arc = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 100)
radius:100.0
startAngle:0
endAngle:180
clockwise:YES];
// 矩形
UIBezierPath *rect = [UIBezierPath bezierPathWithRect:CGRectMake(20, 20, 150, 150)];
// 橢圓
UIBezierPath *oval = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 10, 120, 60)];
// 填色與描邊
[[UIColor redColor] set];
path.lineWidth = 5;
[path fill];
[[UIColor yellowColor] set];
[path stroke];
繪圖過程中,線寬、顏色、對齊方式、裁剪區域等狀態都會影響後續繪製。需要臨時修改又不希望污染後續邏輯時,用 CGContextSaveGState 入棧,用完再以 CGContextRestoreGState 出棧還原。這是 Core Graphics 中最值得養成習慣的一個細節。
另外提醒:在 ARC 環境下,CGContextRef 這類 Core Graphics 對象不屬於 ARC 管理範圍,手動 CGPathCreate、CGGradientCreate 得到的對象都需要對應的 Release,否則會造成內存洩漏。