ios CGContextRef填充颜色

ios CGContextRef填充颜色,ios,objective-c,iphone,Ios,Objective C,Iphone,我想画一条弧并填充它。第一张图片就是它的样子。我想得到第二幅画的效果 - (id)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { // 必须清空背景色,不然绘制出来的区域之外有黑色背景 [self setBackgroundColor:[UIColor clearColor]]; [self setUserInteractionEnabled:NO]; }

我想画一条弧并填充它。第一张图片就是它的样子。我想得到第二幅画的效果

- (id)initWithFrame:(CGRect)frame {
  if (self = [super initWithFrame:frame]) {
      // 必须清空背景色,不然绘制出来的区域之外有黑色背景
      [self setBackgroundColor:[UIColor clearColor]];
      [self setUserInteractionEnabled:NO];
  }
  return self;
}

- (void)drawRect:(CGRect)rect {
    float x = rect.origin.x;
    float y = rect.origin.y;
    float w = rect.size.width;
    float h = rect.size.height;
    CGContextRef context = UIGraphicsGetCurrentContext();
    UIColor *fullColor = [UIColor whiteColor];
    CGContextSetFillColorWithColor(context, fullColor.CGColor);
    CGContextSetRGBStrokeColor(context,1,1,1,1);
    CGContextMoveToPoint(context,0,h - 22);//圆弧的起始点
    CGContextAddQuadCurveToPoint(context, w / 2, h, w, h - 22);
    CGContextMoveToPoint(context,0,h - 22);//圆弧的起始点
    CGContextAddLineToPoint(context, 0, h);
    CGContextAddLineToPoint(context, w, h);
    CGContextAddLineToPoint(context, w, h - 22);

    CGContextStrokePath(context);
    CGContextDrawPath(context, kCGPathFillStroke);
}

我找到了一个解决方案。

CGContextStrokePath()
清除当前路径。这也适用于
CGContextDrawPath()
cgContextOfillPath()
,以及
CGContextFillPath()

因此,当调用
CGContextDrawPath(context,kCGPathFillStroke)时时,上下文中的当前路径刚刚被清除,是空路径,未绘制任何内容

删除行
CGContextStrokePath(上下文)应该可以解决您的问题

- (void)drawRect:(CGRect)rect {
   float x = rect.origin.x;
   float y = rect.origin.y;
   float w = rect.size.width;
   float h = rect.size.height;
   CGContextRef context = UIGraphicsGetCurrentContext();
   CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
   CGContextFillRect(context, CGRectMake(0, h - 22, kScreenWidth , 22));
   CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
   // This method
   CGContextSetBlendMode(context, kCGBlendModeClear);
   CGContextMoveToPoint(context,0,h - 22);
   CGContextAddQuadCurveToPoint(context, w / 2, h + 14, w, h - 22);
   CGContextDrawPath(context, kCGPathFill);
   // This method
   CGContextSetBlendMode(context, kCGBlendModeNormal);
  }