代码之家  ›  专栏  ›  技术社区  ›  RyJ

CGContext线条绘制:CGContextFillPath不工作?

  •  12
  • RyJ  · 技术社区  · 14 年前

    有人知道为什么CGContextFillPath在下面的代码段中不起作用吗?我使用下面的代码来绘制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();
    
    4 回复  |  直到 14 年前
        1
  •  5
  •   tia    14 年前

    我想你需要打电话 CGContextClosePath 在你能填满这条路之前。我不知道你想画什么,但是 CGContextFillPath

        2
  •  27
  •   Alex Kantaev    13 年前

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

    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);
    
        3
  •  19
  •   Mazyod    12 年前

        CGContextDrawPath(myContext, kCGPathFillStroke);
    
        4
  •  9
  •   Mazyod    11 年前

    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 );
    
    推荐文章