Despite looking over more than 3 post (made in 2015) about this problem, non have solved mine.
When ever I run, a simple, code to draw a line using UIBezierPath the program would return me this:
: CGContextSetStrokeColorWithColor: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
: CGContextSetFillColorWithColor: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
: CGContextSetLineWidth: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
: CGContextSetLineJoin: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
: CGContextSetLineCap: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
: CGContextSetMiterLimit: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
: CGContextSetFlatness: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
: CGContextAddPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
: CGContextDrawPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
: CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
@interface line : UIView
UIBezierPath *myPath;
.........
@implementation
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches]anyObject];
CGPoint touchLocation = [touch locationInView:self];
myPath = [UIBezierPath bezierPath];
[myPath moveToPoint:touchLocation];
}
- (void) touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches]anyObject];
CGPoint touchLocation = [touch locationInView:self];
[myPath addLineToPoint:touchLocation];
[[UIColor blackColor]setStroke];
[[UIColor greenColor]setFill];
[myPath stroke];
}
If I was to draw using drawRect
- (void) drawRect:(CGRect)rect {
....
}
No error would pop up. I was wondering If I was receiving these errors because touchesBegan
and touchesMoved
can't perform drawing?
In cocoa (OS X) I used to use setNeedsDisplay
, but it doesn't exist in cocoa touch.
All I'm asking is, is there anyway to remove these errors or is there another way to draw UIBezierPath
during run time.
Invalid context errors happen, because you are trying to use drawing operations outside of drawRect:
method, so no current graphic context is set.
Like on OS X, you should perform drawing in drawRect:
method, and use setNeedsDisplay
to update resulting image. And both setNeedsDisplay
and setNeedsDisplayInRect:
methods are available for UIView on iOS.
So, result should look like this:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches]anyObject];
CGPoint touchLocation = [touch locationInView:self];
myPath = [UIBezierPath bezierPath];
[myPath moveToPoint:touchLocation];
}
- (void) touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches]anyObject];
CGPoint touchLocation = [touch locationInView:self];
[myPath addLineToPoint:touchLocation];
[self setNeedsDisplay];
}
- (void) drawRect:(CGRect)rect {
[[UIColor blackColor]setStroke];
[[UIColor greenColor]setFill];
[myPath stroke];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With