【问题标题】:draw a line in UIImageView problem在 UIImageView 中画一条线的问题
【发布时间】:2009-03-25 09:32:34
【问题描述】:

在我的应用程序中,我可以通过下面的代码在 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;
}

【问题讨论】:

  • 我试过你的代码,它似乎工作正常。
  • 能否请您发布您的完整代码 currentPoint 和 lastPoint 在哪里?声明

标签: iphone cocoa-touch uikit core-graphics


【解决方案1】:

我确定你已经完成了这个,但你有一个错字。

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

应该是:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多