views:

1966

answers:

1

In my application i can draw a line in a UIImageView by the code below,and i want redraw the line even longer when i call the function,however,the output come out is not as expected,it will just draw a new line and remove the old one,the length is remain the same jus the y position different,i dont know which line of my code is wrong or i havent understand the CGContext class in correct way,please help i have scratch my head all the days and cannot find out the problem

- (void) drawLine {
    lastPoint = currentPoint;
    lastPoint.y -= 40;

    //drawImage is a UIImageView declared at header
    UIGraphicsBeginImageContext(drawImage.frame.size);
    [drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)];

    //sets the style for the endpoints of lines drawn in a graphics context
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetLineCap(ctx, kCGLineCapButt);
    //sets the line width for a graphic context
    CGContextSetLineWidth(ctx,2.0);
    //set the line colour
    CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
    //creates a new empty path in a graphics context
    CGContextBeginPath(ctx);
    //begin a new path at the point you specify
    CGContextMoveToPoint(ctx, currentPoint.x, currentPoint.y);
    //Appends a straight line segment from the current point to the provided point 
    CGContextAddLineToPoint(ctx, currentPoint.x,lastPoint.y);
    //paints a line along the current path
    CGContextStrokePath(ctx);
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    currentPoint = lastPoint;
}
+1  A: 

I am sure you are done with this, but you have a typo.

CGContextAddLineToPoint(ctx, currentPoint.x,lastPoint.y);

should be:

CGContextAddLineToPoint(ctx, lastPoint.x,lastPoint.y);

a guy