1

你能帮助我吗 ??我的触摸事件中的某些内容无法正常工作。我的绘画区域有 900 x 700px,不是完整的 UIView。在控制台错误中告诉我:

  CGContextAddLineToPoint: invalid context 0x0
  CGContextMoveToPoint: invalid context 0x0
  CGContextSetRGBStrokeColor: invalid context 0x0
  CGContextSetLineWidth: invalid context 0x0......
  .......
  .......

为什么 UIGraphicsGetCurrentContext 不正确?

我该如何解决这个错误

我的代码:

- (void)**touchesMoved**:(NSSet *)touches withEvent:(UIEvent *)event {

mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.tempDrawImage];

UIGraphicsBeginImageContext(self.tempDrawImage.frame.size);
[self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.tempDrawImage.frame.size.width, self.tempDrawImage.frame.size.height)];

CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);

CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush );
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);

CGContextStrokePath(UIGraphicsGetCurrentContext());
self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
[self.tempDrawImage setAlpha:opacity];
UIGraphicsEndImageContext();

lastPoint = currentPoint;
4

1 回答 1

0

之前已经回答过这个问题 看这个链接: invalid context error

我已经对视图进行了子类化并将我的代码放在 drawRect 方法中,它对我有用。

于 2013-03-31T00:42:11.063 回答