Ios UIBezierPath给出CGContextRestoreGState错误

Ios UIBezierPath给出CGContextRestoreGState错误,ios,objective-c,Ios,Objective C,滚动视图上的简单线条: UIBezierPath *path = [UIBezierPath bezierPath]; [path moveToPoint:CGPointMake(1382.0, 144.0)]; [path addLineToPoint:CGPointMake(1655.0, 152.0)]; path.lineWidth = 2; [path fill]; 给出了其中许多: CGContextRestoreGState: invalid

滚动视图上的简单线条:

 UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(1382.0, 144.0)];
    [path addLineToPoint:CGPointMake(1655.0, 152.0)];
     path.lineWidth = 2;
    [path fill];
给出了其中许多:

CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
用于:

我怎么了

编辑: 这也会导致崩溃:

   UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(1382.0, 144.0)];
    [path addLineToPoint:CGPointMake(1655.0, 152.0)];
    path.lineWidth = 2;

    CAShapeLayer *myLayer = (CAShapeLayer*) self.scroller.layer;
    UIBezierPath *testPath = [UIBezierPath bezierPathWithRect:self.view.bounds];
    [testPath appendPath:path];
    myLayer.path = testPath.CGPath;
    myLayer.fillColor = [UIColor blackColor].CGColor;
    [self.scroller.layer addSublayer:myLayer];


[CALayer setPath:]: unrecognized selector sent to instance

错误是您不能只调用
fill
。仅在有效上下文中使用(例如在
UIView
子类的
drawRect
中或在图像或PDF上下文中)。创建一个
UIView
子类并在其
drawRect
中实现,或者创建一个
CAShapeLayer
并设置其
path
属性


有关这些不同方法的讨论,请参阅。

错误是您不能只调用
fill
。仅在有效上下文中使用(例如在
UIView
子类的
drawRect
中或在图像或PDF上下文中)。创建一个
UIView
子类并在其
drawRect
中实现,或者创建一个
CAShapeLayer
并设置其
path
属性


有关这些不同方法的讨论,请参阅。

设置贝塞尔路径时,是否可以检查滚动视图框?检查框是什么意思?当调用
–fill
方法时,正在绘制的点位于其框内,这意味着您没有上下文。谢谢,我想我没有抓住要点,那么我该怎么办呢?设置bezier路径时可以检查滚动视图框吗?检查框是什么意思?当您调用
–fill
方法时,正在绘制的点在其框内,这意味着您没有上下文。谢谢,我想我没有抓住要点,那我该怎么办呢?你的
CAShapeLayer*myLayer=(CAShapeLayer*)self.scroller.layer应该是
CAShapeLayer*myLayer=[CAShapeLayer层]。您想创建一个
CAShapeLayer
,但您的代码正在获取现有的
CALayer
。您的
CAShapeLayer*myLayer=(CAShapeLayer*)self.scroller.layer应该是
CAShapeLayer*myLayer=[CAShapeLayer层]。您想创建一个
CAShapeLayer
,但您的代码正在获取现有的
CALayer
   UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(1382.0, 144.0)];
    [path addLineToPoint:CGPointMake(1655.0, 152.0)];
    path.lineWidth = 2;

    CAShapeLayer *myLayer = (CAShapeLayer*) self.scroller.layer;
    UIBezierPath *testPath = [UIBezierPath bezierPathWithRect:self.view.bounds];
    [testPath appendPath:path];
    myLayer.path = testPath.CGPath;
    myLayer.fillColor = [UIColor blackColor].CGColor;
    [self.scroller.layer addSublayer:myLayer];


[CALayer setPath:]: unrecognized selector sent to instance