一、如果是自定義一個(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];