iOS繪圖及貝塞爾曲線關(guān)鍵知識(shí)

一、如果是自定義一個(gè)繼承于UIView的子類要在這個(gè)子類view上畫圖的話:1.假如是在drawRect:方法中單純地畫貝塞爾曲線?

- (void)set;

- (void)setFill;

- (void)setStroke;

這三個(gè)方法是用來(lái)設(shè)置這種畫線方式所需的邊色或者填充色(是類UIColor的實(shí)例方法)UIBezierPath *path = [UIBezierPath bezierPath];

[path stroke];//這個(gè)方法必須要寫的,否則不會(huì)畫出來(lái)(描邊)

[path fill];//這個(gè)也要寫,否則不會(huì)畫線(填充)

2.假如畫完線之后(不論是否在drawRect:方法中),所畫貝塞爾曲線的CGPath屬性是用作CAShapeLayer的path屬性的那么[path stroke]、[path fill]這兩個(gè)方法可以不要,(1)如果非要寫的話畫線的代碼需要放到

UIGraphicsBeginImageContext(self.view.bounds.size);

UIGraphicsEndImageContext();

這兩句代碼之間才能避免控制臺(tái)打印出一串

“Jul? 6 14:32:00? TextBeiSaiEr[7433]: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

Jul? 6 14:32:00? TextBeiSaiEr[7433]: CGContextSetLineWidth: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

Jul? 6 14:32:00? TextBeiSaiEr[7433]: CGContextSetLineJoin: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

Jul? 6 14:32:00? TextBeiSaiEr[7433]: CGContextSetLineCap: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

Jul? 6 14:32:00? TextBeiSaiEr[7433]: CGContextSetMiterLimit: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

Jul? 6 14:32:00? TextBeiSaiEr[7433]: CGContextSetFlatness: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

Jul? 6 14:32:00? TextBeiSaiEr[7433]: CGContextAddPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

Jul? 6 14:32:00? TextBeiSaiEr[7433]: CGContextDrawPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

Jul? 6 14:32:00? TextBeiSaiEr[7433]: CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

這些警告。

如果在這兩句代碼之間用

CGContextRef contextRef =UIGraphicsGetCurrentContext();

CGContextAddEllipseInRect(contextRef, CGRectMake(0,0,100,100));

CGContextSetFillColorWithColor(contextRef,? [UIColor blueColor].CGColor);

?CGContextFillPath(contextRef);

來(lái)畫線的話,下面這句代碼是必須的

UIImage* image = UIGraphicsGetImageFromCurrentImageContext();

(2)如果不寫[path stroke]、[path fill]這兩個(gè)方法的話

UIGraphicsBeginImageContext(self.view.bounds.size);

UIGraphicsEndImageContext();

這兩句代碼也可以不需要

二、如果不自定義UIView的子類

可以直接按一中的第2點(diǎn)來(lái)處理

代碼如下:(寫在viewDidLoad方法中的)

UIBezierPath* path = [UIBezierPath bezierPath];

path.lineWidth =3.0;

path.lineCapStyle =kCGLineCapRound;//終點(diǎn)樣式

path.lineJoinStyle =kCGLineJoinRound;//連接點(diǎn)樣式

[path moveToPoint:CGPointMake(100,20)];//起點(diǎn)

[path addLineToPoint:CGPointMake(200,40)];//途經(jīng)點(diǎn)

[path addLineToPoint:CGPointMake(160,140)];//途經(jīng)點(diǎn)

[path addLineToPoint:CGPointMake(40,140)];//途經(jīng)點(diǎn)

[path addLineToPoint:CGPointMake(0,40)];//途經(jīng)點(diǎn)

[path closePath];//閉合

CAShapeLayer * layer = [CAShapeLayer layer];

layer.path = path.CGPath;

layer.backgroundColor = [UIColor clearColor].CGColor;

layer.fillColor = [UIColor clearColor].CGColor;

layer.lineCap =kCALineCapRound;

layer.lineJoin =kCALineJoinRound;

layer.strokeColor = [UIColor redColor].CGColor;

layer.lineWidth = path.lineWidth;

self.view.layer.shouldRasterize = YES;//去線條鋸齒

[self.view.layer addSublayer:layer];

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容