一、如果是自定义一个继承于UIView的子类
要在这个子类view上画图的话:
1.假如是在drawRect:方法中单纯地画贝塞尔曲线
// Set the color: Sets the fill and stroke colors in the current drawing context. Should be implemented by subclassers.
- (void)set;
// Set the fill or stroke colors individually. These should be implemented by subclassers.
- (void)setFill;
- (void)setStroke;
这三个方法是用来设置这种画线方式所需的边色或者填充色(是类UIColor的实例方法)
UIBezierPath* aPath = [UIBezierPathbezierPath];
[aPath stroke];//这个方法必须要写的 否则不会画出来(描边)否则不会画线
[aPath fill];(填充)
那么[aPath stroke]、[aPath fill]这两个方法可以不要,
(1)如果非要写的话画线的代码需要放到
UIGraphicsBeginImageContext(self.view.bounds.size);
UIGraphicsEndImageContext();这两句代码之间才能避免控制台打印出一串
Jul 6 14:32:00 TextBeiSaiEr[7433] <Error>: 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] <Error>: 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] <Error>: 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] <Error>: 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] <Error>: 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] <Error>: 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] <Error>: 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] <Error>: CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
如果在这两句代码之间要用
CGContextRef con =UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(con, CGRectMake(0,0,100,100));
CGContextSetFillColorWithColor(con, [UIColorblueColor].CGColor);
CGContextFillPath(con);
这些代码来画线的话,下面这句代码是必须的
UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsBeginImageContext(self.view.bounds.size);
UIGraphicsEndImageContext();
这两句代码不需要
可以直接按一中的第2点来处理
代码如下:(写在viewDidLoad方法中的)
UIBezierPath* aPath = [UIBezierPathbezierPath];
aPath.lineWidth =5.0;
aPath.lineCapStyle =kCGLineCapRound; //线条拐角
aPath.lineJoinStyle =kCGLineCapRound; //终点处理
// Set the starting point of the shape.
[aPath moveToPoint:CGPointMake(100.0,20.0)];
// Draw the lines
[aPath addLineToPoint:CGPointMake(200.0,40.0)];
[aPath addLineToPoint:CGPointMake(160,140)];
[aPath addLineToPoint:CGPointMake(40.0,140)];
[aPath addLineToPoint:CGPointMake(0.0,40.0)];
[aPath closePath];//第五条线通过调用closePath方法得到的
CAShapeLayer * slayer = [CAShapeLayerlayer];
slayer.path = aPath.CGPath;
slayer.backgroundColor = [UIColorclearColor].CGColor;
slayer.fillColor = [UIColorclearColor].CGColor;
slayer.lineCap =kCALineCapRound;
slayer.lineJoin =kCALineJoinRound;
slayer.strokeColor = [UIColorredColor].CGColor;
slayer.lineWidth = aPath.lineWidth;
[self.view.layeraddSublayer:slayer];