please set CG_CONTEXT_SHOW_BACKTRACE environmental variable

后端 未结 1 1058
囚心锁ツ
囚心锁ツ 2021-02-14 08:50

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

1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-14 09:25

    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];
    }
    

    0 讨论(0)
提交回复
热议问题