Why am I getting an “invalid context” error in the following Core Graphics code?

I have the following drawing code:

static int i=10; int x; int y; int x2; int y2; // Drawing code. CGContextRef c = UIGraphicsGetCurrentContext(); CGFloat colour[4] = {1.0f,0.0f,0.0f,1.0f}; CGContextSetStrokeColor(c, colour); CGContextSetLineWidth(c, 1.0); CGContextBeginPath(c); NSLog(@"fired..."); int xline[340] = {30,80,80,20}; int yline[340] = {40,40,20,20}; CGContextBeginPath(c); CGContextMoveToPoint(c, x, y); CGContextAddLineToPoint(c,x2,y2); //CGContextStrokePath(c); for (int j = 0; j <= 3; j++) { x2 = xline[j]; y2 = yline[j]; CGContextAddLineToPoint(c, x2, y2); x = x2; y = y2; } CGContextStrokePath(c); [self setNeedsDisplay]; i++; 

However, when it runs, I get the following errors:

 Wed Aug 10 11:13:05 Mac-Minis-Mac-mini.local LineDraw[1880] <Error>: CGContextBeginPath: invalid context 0x0 Wed Aug 10 11:13:05 Mac-Minis-Mac-mini.local LineDraw[1880] <Error>: CGContextMoveToPoint: invalid context 0x0 Wed Aug 10 11:13:05 Mac-Minis-Mac-mini.local LineDraw[1880] <Error>: CGContextAddLineToPoint: invalid context 0x0 Wed Aug 10 11:13:05 Mac-Minis-Mac-mini.local LineDraw[1880] <Error>: CGContextAddLineToPoint: invalid context 0x0 Wed Aug 10 11:13:05 Mac-Minis-Mac-mini.local LineDraw[1880] <Error>: CGContextAddLineToPoint: invalid context 0x0 Wed Aug 10 11:13:05 Mac-Minis-Mac-mini.local LineDraw[1880] <Error>: CGContextAddLineToPoint: invalid context 0x0 Wed Aug 10 11:13:05 Mac-Minis-Mac-mini.local LineDraw[1880] <Error>: CGContextAddLineToPoint: invalid context 0x0 Wed Aug 10 11:13:05 Mac-Minis-Mac-mini.local LineDraw[1880] <Error>: CGContextDrawPath: invalid context 0x0 

What can cause these errors and how can I solve this problem?

+4
source share
2 answers

If you do not have this code in the drawRect: method of a UIView subclass or specifically pushed the graphics context onto the stack (for example, using UIGraphicsBeginImageContext ), UIGraphicsGetCurrentContext() will return NULL .

All drawing functions require a graphics context.

+8
source

If you want to receive information on how to draw a rectangle, etc., then this link in which you will get all the information.

+1
source

All Articles