0

I use UIBezierPath to draw a line. But having an error This is my code:

UIBezierPath *aPath = [UIBezierPath bezierPath];
[aPath moveToPoint:CGPointMake(100, 100)];
[aPath addLineToPoint:CGPointMake(200, 200)];
[aPath closePath];
aPath.lineWidth = 5;
[[UIColor redColor] setStroke];
[aPath stroke];

This is error:

CGContextSetStrokeColorWithColor: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.

Mateusz Grzejek
  • 11,698
  • 3
  • 32
  • 49
thinhvd
  • 89
  • 1
  • 11

3 Answers3

1

Rather than

[[UIColor redColor] setStroke];

use

CGContextSetStrokeColorWithColor(context, [[UIColor redColor] CGColor]);

Also you could do the same as-

CAShapeLayer *shapeLayer = [CAShapeLayer layer];
UIBezierPath *aPath;

aPath = [UIBezierPath bezierPath];
[aPath moveToPoint:CGPointMake(0, 0)];
[aPath addLineToPoint:CGPointMake(size*2, 0)];
[aPath addLineToPoint:CGPointMake(size, size*2)];
[aPath closePath];

shapeLayer.path = aPath.CGPath;
shapeLayer.strokeColor = [[UIColor redColor] CGColor];
shapeLayer.fillColor = color;
shapeLayer.lineWidth = width;

[self addSublayer:shapeLayer];
Sanjay Mohnani
  • 5,947
  • 30
  • 46
0

Add this like to setStrokeColor

CGContextSetStrokeColorWithColor(context, [[UIColor redColor] CGColor]);

Mrunal
  • 13,982
  • 6
  • 52
  • 96
0

Try this: In xcode add symbolic breakpoint to CGPostError. (Add symbolic breakpoint, and to Symbol field type CGPostError)

When error happen, debugger will stop code executions and you can check stack of methods calls and check parameters.

Also make sure you have imported QuartzCode framework.

iAhmed
  • 6,556
  • 2
  • 25
  • 31