CoreGraphics - 两个四边形路径之间的可见间隙

CoreGraphics - Gap visible between two Quadrilaterals shaped path

我使用 CoreGraphics 绘制了两个四边形(4 条边)形状的路径。总共有6个点,path1使用前4个点,path2使用后4个点,所以都共享2个点。

代码如下

- (void)drawRect:(CGRect)rect {

    CGPoint topLeft = CGPointMake(121, 116);
    CGPoint topRight = CGPointMake(221, 216);

    CGPoint middleLeft = CGPointMake(121, 180);
    CGPoint middleRight = CGPointMake(221, 280);

    CGPoint bottomLeft = CGPointMake(121, 244);
    CGPoint bottomRight = CGPointMake(221, 344);

    CGMutablePathRef subpath1 = CGPathCreateMutable();
    CGPathMoveToPoint(subpath1, NULL, topLeft.x, topLeft.y);
    CGPathAddLineToPoint(subpath1, NULL, topRight.x, topRight.y);
    CGPathAddLineToPoint(subpath1, NULL, middleRight.x, middleRight.y);
    CGPathAddLineToPoint(subpath1, NULL, middleLeft.x, middleLeft.y);
    CGPathAddLineToPoint(subpath1, NULL, topLeft.x, topLeft.y);

    CGMutablePathRef subpath2 = CGPathCreateMutable();
    CGPathMoveToPoint(subpath2, NULL, middleLeft.x, middleLeft.y);
    CGPathAddLineToPoint(subpath2, NULL, middleRight.x, middleRight.y);
    CGPathAddLineToPoint(subpath2, NULL, bottomRight.x, bottomRight.y);
    CGPathAddLineToPoint(subpath2, NULL, bottomLeft.x, bottomLeft.y);
    CGPathAddLineToPoint(subpath2, NULL, middleLeft.x, middleLeft.y);

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0.19 green:0.42 blue:0.09 alpha:1.0].CGColor);
    CGContextSetBlendMode(context, kCGBlendModeMultiply);
    CGContextSetAlpha(context, 1.0);

    CGContextAddPath(context, subpath1);
    CGContextFillPath(context);

    CGContextAddPath(context, subpath2);
    CGContextFillPath(context);
}

输出图像为

但是在屏幕的连接边缘出现了一条奇怪的白线。我想去掉白线。

任何人都可以帮助如何避免这条白线?

首先将两条路径添加到上下文中,然后再填充它。

- (void)drawRect:(CGRect)rect {

    CGPoint topLeft = CGPointMake(121, 116);
    CGPoint topRight = CGPointMake(221, 216);

    CGPoint middleLeft = CGPointMake(121, 180);
    CGPoint middleRight = CGPointMake(221, 280);

    CGPoint bottomLeft = CGPointMake(121, 244);
    CGPoint bottomRight = CGPointMake(221, 344);

    CGMutablePathRef subpath1 = CGPathCreateMutable();
    CGPathMoveToPoint(subpath1, NULL, topLeft.x, topLeft.y);
    CGPathAddLineToPoint(subpath1, NULL, topRight.x, topRight.y);
    CGPathAddLineToPoint(subpath1, NULL, middleRight.x, middleRight.y);
    CGPathAddLineToPoint(subpath1, NULL, middleLeft.x, middleLeft.y);
    CGPathAddLineToPoint(subpath1, NULL, topLeft.x, topLeft.y);

    CGMutablePathRef subpath2 = CGPathCreateMutable();
    CGPathMoveToPoint(subpath2, NULL, middleLeft.x, middleLeft.y);
    CGPathAddLineToPoint(subpath2, NULL, middleRight.x, middleRight.y);
    CGPathAddLineToPoint(subpath2, NULL, bottomRight.x, bottomRight.y);
    CGPathAddLineToPoint(subpath2, NULL, bottomLeft.x, bottomLeft.y);
    CGPathAddLineToPoint(subpath2, NULL, middleLeft.x, middleLeft.y);

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0.19 green:0.42 blue:0.09 alpha:1.0].CGColor);
    CGContextSetBlendMode(context, kCGBlendModeMultiply);
    CGContextSetAlpha(context, 1.0);

    // Changes start here...
    CGContextAddPath(context, subpath1);
    CGContextAddPath(context, subpath2);
    CGContextFillPath(context);
}