Objective C中的CGContextFillPath如何工作?

Kar*_*sis 2 graphics cocoa-touch objective-c cgcontext ios

我有以下代码,它应该绘制一个描边和填充矩形,但填充不会显示.

    [[UIColor greenColor] setStroke];
    [[UIColor brownColor] setFill];

    CGContextBeginPath(context);
    CGContextMoveToPoint(context, right, bottom);
    CGContextAddLineToPoint(context, right,top);
    CGContextAddLineToPoint(context,left, top);
    CGContextAddLineToPoint(context, left, bottom);
    CGContextAddLineToPoint(context, right, bottom);

    CGContextStrokePath(context);
    CGContextFillPath(context);
Run Code Online (Sandbox Code Playgroud)

中风工作,我得到一个漂亮的绿色矩形,没有填充(或白色填充).这是在UIView for iOS中.看起来非常简单,它让我疯了!

Eli*_*Eli 6

正确的方法是将绘图模式设置为包括填充和路径.

CGPathDrawingMode mode = kCGPathFillStroke;
CGContextClosePath( context ); //ensure path is closed, not necessary if you know it is
CGContextDrawPath( context, mode );
Run Code Online (Sandbox Code Playgroud)

您可以使用CGContextFillPath()简单地做了填充,但它基本上只是CGContextDrawPath()自动调用CGContextClosePath()并使用kCGPathFillCGPathDrawingMode.您也可以随时使用CGContextDrawPath()并输入自己的参数来获得所需的填充/描边类型.您还可以使用其中一种偶数绘图模式在凸路径中放置孔.