要做自訂圖表、進度環、漸層背景時,Core Graphics 是繞不開的工具。所有繪圖都從取得圖形上下文開始,以下把常用函式依用途分類整理。
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextMoveToPoint // 移動畫筆到起點,開始一條路徑
CGContextAddLineToPoint // 從目前位置畫直線到指定點
CGContextAddRect // 加入矩形路徑
CGContextAddRects // 一次加入多個矩形
CGContextStrokeRect // 描繪指定矩形
CGContextStrokeRectWithWidth // 描繪矩形並指定線寬
CGContextStrokeLineSegments // 一次畫多段直線
CGContextAddEllipseInRect // 加入橢圓路徑
CGContextAddQuadCurveToPoint // 加入二次貝茲曲線
CGContextAddArc // 畫圓弧:前兩個參數為圓心,接著是半徑與起始/結束弧度
CGContextAddArcToPoint // 依兩個控制點畫出帶圓角的轉折
CGContextClosePath // 封閉目前路徑
CGContextStrokePath // 描邊
CGContextDrawPath // 依指定模式描邊與/或填滿
CGContextClip // 以目前路徑作為後續繪製的裁剪範圍
CGContextAddArc 的最後一個參數是方向旗標:0 為順時針,1 為逆時針。
CGContextSetLineWidth // 線寬
CGContextSetLineCap // 線條端點樣式(butt / round / square)
CGContextSetLineDash // 虛線樣式
CGContextSetRGBStrokeColor // 描邊顏色
CGContextSetRGBFillColor // 填滿顏色
CGContextSetAlpha // 透明度
CGContextSetShadowWithColor // 陰影
CGContextSetFillColorSpace // 填色色彩空間
CGContextSetStrokeColorSpace // 描邊色彩空間
CGContextSetInterpolationQuality // 影像內插品質
CGContextFillRect // 填滿指定矩形
CGContextFillRects // 填滿多個矩形
CGContextFillEllipseInRect // 填滿矩形內的橢圓
CGContextFillPath // 以「非零環繞數規則」填滿目前路徑
CGContextEOFillPath // 以「奇偶規則」填滿目前路徑
CGContextTranslateCTM(context, 0, rect.size.height);
CGContextScaleCTM(context, 1.0, -1.0); // 上下翻轉畫布,方便用左上原點思考
這個組合很常用:Core Graphics 原點在左下角,而 UIKit 在左上角,翻轉一次之後就能用熟悉的座標系作圖。
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);
// 畫圖片
CGImageRef image = CGImageRetain(img.CGImage);
CGContextDrawImage(context, CGRectMake(10.0, height - 100.0, 90.0, 90.0), image);
// 從原圖裁出小圖
CGImageRef sub = CGImageCreateWithImageInRect(image, CGRectMake(0, 0, 45, 45));
// 寫文字:NSString 本身就有繪圖方法
- (CGSize)drawInRect:(CGRect)rect
withFont:(UIFont *)font
lineBreakMode:(UILineBreakMode)lineBreakMode
alignment:(UITextAlignment)alignment;
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGColorGetComponents(colorRef); // 回傳陣列,含各分量與透明度
當多條子路徑重疊時,決定某個點是否被填滿有兩套規則:
非零環繞數規則(nonzero winding number):從左側射出一條線,向右穿越記 +1、向左穿越記 −1。最終不為零則填滿。對應 CGContextFillPath。
奇偶規則(even-odd):不計方向,每穿越一次就 +1。奇數填滿、偶數不填。對應 CGContextEOFillPath。這也是做出「甜甜圈」效果的關鍵。
預設的混合公式為 result = (alpha * foreground) + (1 - alpha) * background。要自訂時:
CGContextSetBlendMode(context, kCGBlendModeMultiply); // 設定
CGContextSaveGState(context); // 保存狀態
CGContextRestoreGState(context); // 還原狀態
畫完路徑後,若要同時有輪廓與填色,必須先 StrokePath 描邊,再 FillPath 填色。順序顛倒會導致填色蓋掉一半的線條。另外所有 CGColorSpaceCreate、CGGradientCreate、CGImageCreate 建立的物件都不受 ARC 管理,用完記得 Release。