【问题标题】:CGContext line drawing: CGContextFillPath not working?CGContext 线图:CGContextFillPath 不起作用?
【发布时间】:2011-04-18 01:30:26
【问题描述】:

有人知道为什么 CGContextFillPath 在下面的代码 sn-p 中不起作用吗?我正在使用以下代码绘制到 UIImageView。它正确地抚摸路径,但忽略 CGContextFillPath。

UIGraphicsBeginImageContext(self.frame.size);
[drawingView.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextSetRGBFillColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), firstMovedTouch.x, firstMovedTouch.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFillPath(UIGraphicsGetCurrentContext());
drawingView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

【问题讨论】:

    标签: iphone quartz-2d cgcontext


    【解决方案1】:

    我也遇到了同样的问题。我发现你不能用

    CGContextStrokePath( UIGraphicsGetCurrentContext() ) 
    CGContextFillPath( UIGraphicsGetCurrentContext() ) 
    

    连续:后者将不起作用,因为在路径上提交笔画后,路径从上下文中删除。

    所以,我两次使用了相同的路径;一个用于描边,另一个用于填充。

    // ... create a path of CGMutablePathRef ....
    
    CGContextSaveGState( context );
    //....fill the path .....
    CGContextRestoreGState( context );
    
    CGContextSaveGState ( context );
    //....stroke the path .....
    CGContextRestoreGState( context );
    
    CFRelease( path );
    

    【讨论】:

    • 谢谢。经过多年的问题拍摄,这救了我
    • 它在文档中:当您想将路径附加到图形上下文时,您调用函数 CGContextAddPath。在 Quartz 绘制它之前,路径会一直保留在图形上下文中。您可以通过调用 CGContextAddPath 再次添加路径。
    【解决方案2】:

    我相信描边和填充路径的最佳方法是在完成路径设置后添加以下代码行:

        CGContextDrawPath(myContext, kCGPathFillStroke);
    

    【讨论】:

    • 过去只是看斯坦福 CS193P 课程的问题......我想这是他们介绍这个的第二或第三堂课。
    【解决方案3】:

    要解决此问题,请使用以下代码:

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    
    CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
    CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 1.0);
    CGContextSetLineJoin(context, kCGLineJoinRound);
    CGContextSetLineWidth(context, 8.0);
    
    CGMutablePathRef pathRef = CGPathCreateMutable();
    
    /* do something with pathRef. For example:*/
    CGPathMoveToPoint(pathRef, NULL, x, y);
    CGPathAddLineToPoint(pathRef, NULL, x, y+100);
    CGPathAddLineToPoint(pathRef, NULL, x+100, y+100);
    CGPathAddLineToPoint(pathRef, NULL, x+100, y);
    CGPathCloseSubpath(pathRef);
    
    CGContextAddPath(context, pathRef);
    CGContextFillPath(context);
    
    CGContextAddPath(context, pathRef);
    CGContextStrokePath(context);
    
    CGPathRelease(pathRef);
    

    【讨论】:

      【解决方案4】:

      我认为您需要先致电CGContextClosePath,然后才能填写路径。我不确定你要画什么,但CGContextFillPath 将填充路径内的区域,我在这里只看到一条线。

      【讨论】:

      • 哦...你是对的。我正在使用 ghetto 手指画线的代码。但这是有道理的。此代码在 touchesMoved: 上触发,非常适合绘制线条图,但我必须将所有点保存到数组或其他东西中,以便在用户抬起手指后填充路径。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多