一、如果是自定义一个继承于UIView的子类要在这个子类view上画图的话:1.假如是在drawRect:方法中单纯地画贝塞尔曲线
- (void)set;
- (void)setFill;
- (void)setStroke;
这三个方法是用来设置这种画线方式所需的边色或者填充色(是类UIColor的实例方法)UIBezierPath *path = [UIBezierPath bezierPath];
[path stroke];//这个方法必须要写的,否则不会画出来(描边)
[path fill];//这个也要写,否则不会画线(填充)
2.假如画完线之后(不论是否在drawRect:方法中),所画贝塞尔曲线的CGPath属性是用作CAShapeLayer的path属性的那么[path stroke]、[path fill]这两个方法可以不要,(1)如果非要写的话画线的代码需要放到
UIGraphicsBeginImageContext(self.view.bounds.size);
UIGraphicsEndImageContext();
这两句代码之间才能避免控制台打印出一串
“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);
来画线的话,下面这句代码是必须的
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
(2)如果不写[path stroke]、[path fill]这两个方法的话
UIGraphicsBeginImageContext(self.view.bounds.size);
UIGraphicsEndImageContext();
这两句代码也可以不需要
二、如果不自定义UIView的子类
可以直接按一中的第2点来处理
代码如下:(写在viewDidLoad方法中的)
UIBezierPath* path = [UIBezierPath bezierPath];
path.lineWidth =3.0;
path.lineCapStyle =kCGLineCapRound;//终点样式
path.lineJoinStyle =kCGLineJoinRound;//连接点样式
[path moveToPoint:CGPointMake(100,20)];//起点
[path addLineToPoint:CGPointMake(200,40)];//途经点
[path addLineToPoint:CGPointMake(160,140)];//途经点
[path addLineToPoint:CGPointMake(40,140)];//途经点
[path addLineToPoint:CGPointMake(0,40)];//途经点
[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];