都是平時常用的東西
不整理一下的話總感覺有點亂
這三種東西:CGContextRef CGPath UIBezierPath
本質(zhì)上都是一樣的
都是使用 Quartz 來繪畫
只不過把繪圖操作暴露在不同的 API 層面上
在具體實現(xiàn)上
也會有一些細(xì)小的差別
1 - UIBezierPath 方式
首先使用 UIBezierPath 類型繪制
UIBezierPath 包裝了Quartz的相關(guān)API
自己存在于UIKit中
因此不是基于C的API
而是基于Objective-C對象的
繪制流程 :
- (void)modeUIBezierPath {
// 開啟一個與屏幕大小一樣的圖形上下文
UIGraphicsBeginImageContext(self.view.bounds.size);
UIBezierPath *path = [UIBezierPath bezierPath];
[path appendPath:[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 20, 20)]];
[path appendPath:[UIBezierPath bezierPathWithOvalInRect:CGRectMake(80, 0, 20, 20)]];
[path moveToPoint:CGPointMake(100, 50)];
// 注意這里 clockwise 參數(shù)是 YES 而不是 NO,如果是 Quartz,需要考慮Y軸翻轉(zhuǎn)的問題傳 NO
[path addArcWithCenter:CGPointMake(50, 50) radius:50 startAngle:0 endAngle:M_PI clockwise:YES];
// 可以使用 applyTransform 函數(shù)來轉(zhuǎn)移坐標(biāo)的 Transform
[path applyTransform:CGAffineTransformMakeTranslation(150, 200)];
[[UIColor redColor] setStroke];
[path setLineWidth:2];
[path stroke];
// 從 Context 中獲取圖像,并顯示在界面上
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
imgView.backgroundColor = [UIColor blackColor];
[self.view addSubview:imgView];
}
效果 :

效果圖
2 - CGContextRef 方式 :
- (void)modeCGContextRef {
// 開啟一個與屏幕大小一樣的圖形上下文
UIGraphicsBeginImageContext(self.view.bounds.size);
// 獲取當(dāng)前 CGContextRef
CGContextRef gc = UIGraphicsGetCurrentContext();
// 可以使用 CGContextTranslateCTM 函數(shù)來轉(zhuǎn)移坐標(biāo)的 Transform
CGContextTranslateCTM(gc, 150, 200);
CGContextAddEllipseInRect(gc, CGRectMake(0, 0, 20, 20));
CGContextAddEllipseInRect(gc, CGRectMake(80, 0, 20, 20));
CGContextMoveToPoint(gc, 100, 50);
CGContextAddArc(gc, 50, 50, 50, 0, M_PI, NO);
[[UIColor redColor] setStroke];
CGContextSetLineWidth(gc, 2);
CGContextStrokePath(gc);
// 從 Context 中獲取圖像,并顯示在界面上
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
imgView.backgroundColor = [UIColor blackColor];
[self.view addSubview:imgView];
}
效果 :

效果圖
3 - CGPath 方式 :
- (void)modeCGPath {
// 開始圖像繪圖
UIGraphicsBeginImageContext(self.view.bounds.size);
// 獲取當(dāng)前 CGContextRef
CGContextRef gc = UIGraphicsGetCurrentContext();
// 可以使用 CGAffineTransformMakeTranslation 函數(shù)來轉(zhuǎn)移坐標(biāo)的 Transform
CGAffineTransform transform = CGAffineTransformMakeTranslation(150, 200);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddEllipseInRect(path, &transform, CGRectMake(0, 0, 20, 20));
CGPathAddEllipseInRect(path, &transform, CGRectMake(80, 0, 20, 20));
CGPathMoveToPoint(path, &transform, 100, 50);
CGPathAddArc(path, &transform, 50, 50, 50, 0, M_PI, NO);
// 將 CGMutablePathRef 添加到當(dāng)前 Context 內(nèi)
CGContextAddPath(gc, path);
[[UIColor redColor] setStroke];
CGContextSetLineWidth(gc, 2);
CGContextStrokePath(gc);
// 從 Context 中獲取圖像,并顯示在界面上
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
imgView.backgroundColor = [UIColor blackColor];
[self.view addSubview:imgView];
}
效果 :

效果圖
另外 :
由于三種方式本質(zhì)上都是一樣的
都是使用Quartz來繪畫
使用中很多地方都是互通的
比如 CGPath 和 UIBezierPath 的互轉(zhuǎn)
CGPathRef cgPath = CGPathCreateMutable();
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithCGPath:cgPath];
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithCGPath:cgPath];
CGPathRef cgPath = bezierPath.CGPath;
所以上面任何用 CGPath 的地方都可以換成 UIBezierPath
相反也同理