2009-03-25 12 views
1

Dans mon application, je peux dessiner une ligne dans un UIImageView par le code ci-dessous, et je veux redessiner la ligne encore plus longtemps quand j'appelle la fonction, cependant, la sortie sort n'est pas comme prévu, il va juste dessiner une nouvelle ligne et enlever l'ancienne, la longueur reste la même jus la position y différente, je ne sais pas quelle ligne de mon code est faux ou je n'ai pas compris la classe CGContext de manière correcte, s'il vous plaît aidez-moi gratter ma tête tous les jours et ne peut pas trouver le problèmetracer une ligne dans UIImageView problème

- (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; 
} 
+0

J'ai essayé votre code, et il semble fonctionner très bien. –

+0

pourriez-vous s'il vous plaît poster votre code complet où sont le currentPoint et lastPoint; déclaré –

Répondre

2

Je suis sûr que vous avez fini avec cela, mais vous avez une faute de frappe.

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

devrait être:

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