CGContextAddLineToPoint: no current point

I am developing a template blocking application (e.g. Android lock).

I want to draw lines between the points to open the lock, but when I draw, it returns an error:

<Error>: CGContextAddLineToPoint: no current point

It works fine in iOS 5.0 and earlier, but shows an error in 5.1.

This is my code:

  - (void)drawRect:(CGRect)rect { NSLog(@"drawrect...%@",NSStringFromCGRect(rect)); if (!self._trackPointValue) return; CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetLineWidth(context, 10.0); CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB(); CGFloat components[] = {0.5, 1.0, 0.5, 0.8}; CGColorRef color = CGColorCreate(colorspace, components); CGContextSetStrokeColorWithColor(context, color); CGPoint from; UIView *lastDot; for (UIView *dotView in self._dotViews) { //_dotViews array of points from = dotView.center; if (!lastDot) { CGContextMoveToPoint(context, from.x, from.y); } else { NSLog(@"from : %@",NSStringFromCGPoint(from)); CGContextAddLineToPoint(context, from.x, from.y); } lastDot = dotView; } CGPoint pt = [self._trackPointValue CGPointValue]; //_trackPointValue is current point CGContextAddLineToPoint(context, pt.x, pt.y); CGContextStrokePath(context); CGColorSpaceRelease(colorspace); CGColorRelease(color); self._trackPointValue = nil;//_trackPointValue is current point } 
+7
source share
4 answers

To have a current point, you must make sure that at least once CGContextMoveToPoint been called before CGContextAddLineToPoint has acted.

+11
source

It:

 UIView *lastDot; 

Must be:

 UIView *lastDot = nil; 

Uninitialized automatic variables are garbage. Your code is trying to do something special for the first time through a loop when lastDot is not already set. You need to explicitly point it to nil .

+2
source

First you must create a path using CGContextBeginPath to start adding dots and lines to it.

0
source

When you first arrive, the forin method will not enter. So the method "CGContextMoveToPoint ()" is used, and then they result in a warning.

0
source

All Articles