在 CAShapeLayer 上绘制 bezierPath 时上下文无效

Invalid context when drawing bezierPath on CAShapeLayer

所以当 运行 我的代码时出现这些错误:

CGContextSetStrokeColorWithColor: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
CGContextSetLineWidth: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
CGContextSetLineJoin: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
CGContextSetLineCap: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
CGContextSetMiterLimit: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
CGContextSetFlatness: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
CGContextAddPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
CGContextDrawPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

当我设置 CG_CONTEXT_SHOW_BACKTRACE 环境变量时,错误指向我的 func bezierPath(dentInMM value: Float) -> CGPath 函数。以下是构成我的控制器这一部分的代码片段:

var shape = CAShapeLayer()

func someFunc() {
  self.dentView.layer.addSublayer(self.shape)

  let color: UIColor = .systemGreen
  self.shape.fillColor = color.withAlphaComponent(0.3).cgColor
  self.shape.strokeColor = color.cgColor
  self.shape.lineWidth = 6
  self.shape.path = self.bezierPath(dentInMM: 0)
}

func bezierPath(dentInMM value: Float) -> CGPath {
    self.shape.frame = self.dentView.bounds

    let x000 = Int(self.shape.frame.minX)
    let x050 = Int(self.shape.frame.midX)
    let x100 = Int(self.shape.frame.maxX)
    let x010 = x100 / 10
    let x040 = x050 - x010
    let x060 = x050 + x010
    let yOffSet = Int(self.shape.frame.minY)

    let indentation = Int(value * self.dentDisplayMultiplier)

    let bezierPath = UIBezierPath()
    bezierPath.move(to: CGPoint(x: x000, y: yOffSet))
    bezierPath.addCurve(to: CGPoint(x: x050, y: indentation), controlPoint1: CGPoint(x: x050, y: yOffSet), controlPoint2: CGPoint(x: x040, y: indentation))
    bezierPath.addCurve(to: CGPoint(x: x100, y: yOffSet), controlPoint1: CGPoint(x: x060, y: indentation), controlPoint2: CGPoint(x: x050, y: yOffSet))
    UIColor.label.setStroke()
    bezierPath.stroke()

    return bezierPath.cgPath
}

我需要在某处指定上下文吗? 我的印象是 CAShapeLayer 提供了上下文...

试试这个

let bezierPath = UIBezierPath()

UIGraphicsBeginImageContextWithOptions(self.shape.frame.size, false, 0)

bezierPath.move(to: CGPoint(x: x000, y: yOffSet))
bezierPath.addCurve(to: CGPoint(x: x050, y: indentation), controlPoint1: CGPoint(x: x050, y: yOffSet), controlPoint2: CGPoint(x: x040, y: indentation))
bezierPath.addCurve(to: CGPoint(x: x100, y: yOffSet), controlPoint1: CGPoint(x: x060, y: indentation), controlPoint2: CGPoint(x: x050, y: yOffSet))
UIColor.label.setStroke()
bezierPath.stroke()

UIGraphicsEndImageContext()

不必调用绘图方法

bezierPath.stroke()

CAShapeLayer 会在 dentView 中绘制自己

你真的需要

UIColor.label.setStroke()

因为您已经在 someFunc() 中设置了 strokeColor

这是没有错误的工作:

class ThirdViewController: UIViewController {
    let dentDisplayMultiplier : Float = 6
    var shape = CAShapeLayer()

    @IBOutlet weak var dentView: UIView!


    func someFunc() {
      self.dentView.layer.addSublayer(self.shape)

      let color: UIColor = .systemGreen
      self.shape.fillColor = color.withAlphaComponent(0.3).cgColor
      self.shape.strokeColor = color.cgColor
      self.shape.lineWidth = 6
      self.shape.path = self.bezierPath(dentInMM: 3)
    }

    func bezierPath(dentInMM value: Float) -> CGPath {
        self.shape.frame = self.dentView.bounds

        let x000 = Int(self.shape.frame.minX)
        let x050 = Int(self.shape.frame.midX)
        let x100 = Int(self.shape.frame.maxX)
        let x010 = x100 / 10
        let x040 = x050 - x010
        let x060 = x050 + x010
        let yOffSet = Int(self.shape.frame.minY)

        let indentation = Int(value * self.dentDisplayMultiplier)

        let bezierPath = UIBezierPath()
        bezierPath.move(to: CGPoint(x: x000, y: yOffSet))
        bezierPath.addCurve(to: CGPoint(x: x050, y: indentation), controlPoint1: CGPoint(x: x050, y: yOffSet), controlPoint2: CGPoint(x: x040, y: indentation))
        bezierPath.addCurve(to: CGPoint(x: x100, y: yOffSet), controlPoint1: CGPoint(x: x060, y: indentation), controlPoint2: CGPoint(x: x050, y: yOffSet))

        return bezierPath.cgPath
    }

    override func viewDidLoad() {


        super.viewDidLoad()
        // Do any additional setup after loading the view.
        someFunc()

    }

}