CGContextAddLineToPoint: no current point
Asked Answered
H

4

11

I am developing a pattern lock application, (like the Android lock).

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

<Error>: CGContextAddLineToPoint: no current point

It's working fine in iOS 5.0 and before but it's showing 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
 }
Hetero answered 21/3, 2012 at 6:35 Comment(0)
G
15

To have current point, You have to ensure that at least once CGContextMoveToPoint was called before CGContextAddLineToPoint get in action.

Greenwood answered 3/1, 2013 at 15:58 Comment(2)
by chance, is there anything off the top of your head that would cause that method not to be called? I'm having the same issue, but I"m absolutely sureRecountal
@Recountal - Make sure you are calling CGContextStrokePath() only after the line is complete. If you call it prior to the line being complete, you have to call CGContextMoveToPoint() again.Neldanelia
D
1

You must first create a path with CGContextBeginPath before you can start adding points and lines to it.

Derrickderriey answered 21/3, 2012 at 6:38 Comment(1)
Not true. A CGContext always has a current path (possibly an empty one) which you can add elements to. You need CGContextBeginPath only when you want to discard the current path and start with a new empty path.Entryway
E
1

This:

UIView *lastDot;

Should be:

UIView *lastDot = nil;

Uninitialized automatic variables are garbage. Your code is trying to do something special the first time through the loop, when lastDot has not yet been set. You need to explicitly set it to nil first.

Entryway answered 21/3, 2012 at 6:48 Comment(0)
B
0

When you first come, the forin method won't come in.So there are far use of the method "CGContextMoveToPoint()" , and then result in the warning.

Bezel answered 4/6, 2015 at 10:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.