在UIImageView问题中画一条线

时间:2009-03-25 09:32:35

标签: iphone cocoa-touch uikit core-graphics

在我的应用程序中,我可以通过下面的代码在UIImageView中绘制一条线,我想在调用函数时重新绘制更长的行,但是,输出结果不符合预期,它只会绘制一个新的行和删除旧的,长度是相同的jus y位置不同,我不知道我的代码哪一行是错的或我没有正确理解CGContext类,请帮助我刮了我的头所有的日子并且无法找出问题

- (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 个答案:

答案 0 :(得分:2)

我相信你已经完成了这件事,但你有一个错字。

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

应该是:

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