ios - 为什么在iOS中使用核心图形绘制的线的连接处会出现一个点?

标签 ios core-graphics

我正在使用以下代码绘制多边形。我得到的图像如下:

enter image description here

我用来绘制正方形的代码如下:

CGContextRef currentContext = UIGraphicsGetCurrentContext();
[topLeftBoxBorderColor set];
CGContextSetLineWidth(currentContext, 1.0f);
CGContextSetLineJoin(currentContext,kCGLineJoinMiter);
CGContextMoveToPoint(currentContext, originXOfSubviews, originYOfSubviews);
CGContextAddLineToPoint(currentContext, originXOfSubviews+widthOfBox, originYOfSubviews);
CGContextAddLineToPoint(currentContext, originXOfSubviews+widthOfBox, originYOfSubviews+heightOfBox);
CGContextAddLineToPoint(currentContext, originXOfSubviews, originYOfSubviews+heightOfBox);
CGContextAddLineToPoint(currentContext, originXOfSubviews, originYOfSubviews);
CGContextStrokePath(currentContext);

在所有角的两条线的交界处都有一个白点。有什么办法可以避免有这个点吗?

更新 1:

我知道以下解决方案:根据线宽,可以更改线的起点并实现我的期望。但是还有其他解决办法吗?

更新 2:

我正在使用下面的代码绘制多边形

每当我将 lineWidth 设置为 1 或小于 1 时,都会发生同样的问题。如果我将它设置为 2 或更高,则不会出现问题。

CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(context, 2.0);

    CGContextSetStrokeColorWithColor(context, topLeftBoxBorderColor.CGColor);

    CGRect rectangle = CGRectMake(originXOfSubviews,originYOfSubviews,widthOfBox,heightOfBox);

    CGContextAddRect(context, rectangle);

    CGContextStrokePath(context);

最佳答案

它是两条线(水平线和垂直线)的交点。

你可以做的是让这条线比你现在使用的总长度小几个像素/点。它不会相交,也不会出现点。

编辑:

或者你可以画正方形,而不是画线

- (void)drawSquare:(CGRect)rect{
    //Get the CGContext from this view
    CGContextRef context = UIGraphicsGetCurrentContext();
    //Draw a rectangle
    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
    //Define a rectangle
    CGContextAddRect(context, rect);
    //Draw it
    CGContextFillPath(context); 
}

关于ios - 为什么在iOS中使用核心图形绘制的线的连接处会出现一个点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13680607/

相关文章:

ios - $T7?? Swift 中没有成员 'Generator'

ios - UINavigationController 改变 InteractivePopGesture 方向

ios - 将 CALayer 添加到 MKOverlayRenderer?

objective-c - iOS 上的简单多点触控绘图应用程序 : Too slow (because drawRect() not additive ? )

ios - 向 uiview 添加矩形时出现的问题

ios - 如何在同一个 View Controller 中使用 2 个 UIPickerViews?

ios - Xcode 7 错误 : could not read CFBundleIdentifier from Info. plist(空)

ios - 带有 reloadData 的 AKPickerView 导致奇怪的行为

iphone - iOS:使用 CoreGraphics 进行两步图像处理

objective-c - iOS 核心图形 : Using CGContext setters to render a sharp pdf