UIGraphicsGetCurrentContext 上下线颜色不正确

Incorrect upper and lower line color with UIGraphicsGetCurrentContext

项目使用ASDK(Texture)。有一个主节点,它有一个子节点,在某些情况下需要显示子节点的外边界,在这个项目中是通过添加一个边界节点和UIGraphicsGetCurrentContext来完成的,任务也是边界无论UIScreen.main.scale如何,所有设备上的线宽都相同。我做了一个测试代码,一切正常,除了下线和上线的宽度小于侧线(显示正确)。请告诉如何解决它?我无法传播整个屏幕的屏幕截图,但我上传了部分内容,以便显示问题所在。你可以看到底线上的绿色,这不应该。

private func enableEditingMode() {
        let solidBorderStyle = BorderStyle.solid
        let border = ASBorderNode(shapeType: self.shapeType, borderType: solidBorderStyle)
        border.frame = ASBorderNode.calculateFrame(self.shapeType, borderType: solidBorderStyle, superView: frame)
        supernode?.insertSubnode(border, at: 0)
    }

enum BorderStyle {
    case dashed
    case solid
    case solidLight

    var lineWidth: CGFloat {
        switch self {
        case .dashed:
            return 1
        case .solid:
            return 6 / UIScreen.main.scale
        case .solidLight:
            return 2
        }
    }

}

class ASBorderNode: ASDisplayNode {

    private (set) var shapeType: TemplateLayoutElement.Shape?
    private (set) var borderType: BorderStyle


    init(shapeType:TemplateLayoutElement.Shape?, borderType:BorderStyle = .dashed) {
        self.borderType = borderType
        self.shapeType = shapeType
        super.init()
        self.isOpaque = false
    }

    class func calculateFrame(_ shapeType: TemplateLayoutElement.Shape?, borderType: BorderStyle, superView frame: CGRect) -> CGRect {
        switch borderType {
        case .solid:
            let lineWidth = borderType.lineWidth //* UIScreen.main.scale
            let widthHeightValue = lineWidth * 2
            return CGRect(x: -lineWidth + frame.origin.x, y: -lineWidth + frame.origin.y, width: frame.width + widthHeightValue, height: frame.height + widthHeightValue)
        default:
            assertionFailure("Implement for specific style")
            return .zero
        }
    }

    override class func draw(_ bounds: CGRect, withParameters parameters: Any?, isCancelled isCancelledBlock: () -> Bool, isRasterizing: Bool) {
        if let context = UIGraphicsGetCurrentContext(),
            let parameters = parameters as? ASBorderNode {

            switch parameters.borderType {
            case .dashed:
                break
            case .solid:
                context.setStrokeColor(UIColor.black.withAlphaComponent(0.5).cgColor)
                context.setFillColor(UIColor.clear.cgColor)
                context.setLineWidth(parameters.borderType.lineWidth)
            case .solidLight:
                break
            }

            switch parameters.shapeType {
            case .circle?:
                break
            default:
                switch parameters.borderType {
                case .solid:
                    debugPrint("Bounds", bounds)
                    let frame = bounds.insetBy(dx: parameters.borderType.lineWidth / UIScreen.main.scale, dy: parameters.borderType.lineWidth / UIScreen.main.scale)
                    debugPrint("insetBy", frame)
                    context.addRect(frame)
                default:
                    break
                }
            }

            context.strokePath()
        }
    }

    override func drawParameters(forAsyncLayer layer: _ASDisplayLayer) -> NSObjectProtocol? {
        return self
    }

}

更新 1:另一个例子

我相信您看到的是中风伪影。我知道如何避免它们的唯一方法是构建一条与您想要的笔划形状相对应的路径并使用 fill (这样您就可以将填充的对象叠加在一起,而不仅仅是抚摸基本路径)。