1

私のアプリケーションでは、以下のコードでUIImageViewに線を引くことができます。関数を呼び出すと、線をさらに長く再描画したいのですが、出力が期待どおりではなく、新しい線を描画して削除するだけです。古いもの、長さは同じままですが、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;
}
4

1 に答える 1

2

これで完了だと思いますが、タイプミスがあります。

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

次のようにする必要があります。

CGContextAddLineToPoint(ctx, lastPoint.x,lastPoint.y);
于 2010-02-02T00:23:23.487 に答える