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 prog
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 *)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];
}