本文介绍了在UIImageView问题中画一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我可以通过下面的代码在UIImageView中绘制一条线,我想重新绘制线甚至更长的时间,当我调用该函数,但是,输出不是预期的,它只会绘制一个新的行和删除旧的,长度保持不变jus y位置不同,我不知道我的代码是哪一行是错误或我不能以正确的方式理解CGContext类,请帮我我刮头我的头一天并且找不到问题

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;
}


推荐答案

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

应为:

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

这篇关于在UIImageView问题中画一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 14:37