0

A simple line over a scrollview :

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

gives many of these :

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

for:

CGContextSaveGState ,CGContextSetFlatness,CGContextAddPath,CGContextDrawPath

What is wrong with me ?

EDIT: This also gives a crash:

   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
Curnelious
  • 1
  • 16
  • 76
  • 150

1 Answers1

4

The error is saying that you cannot just call fill. That is intended to be used only within a valid context (e.g. in the drawRect of a UIView subclass or inside an image or PDF context). Either create a UIView subclass and implement this in its drawRect or create a CAShapeLayer and set its path property.

See how do you draw a line programmatically from a view controller? for a discussion of these various approaches.

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • 1
    Your `CAShapeLayer *myLayer = (CAShapeLayer*) self.scroller.layer;` should be `CAShapeLayer *myLayer = [CAShapeLayer layer];`. You want to create a `CAShapeLayer`, but your code was getting the existing `CALayer`. – Rob Jan 19 '16 at 17:08