Swift 在CAShapeLayer上绘制bezierPath时上下文无效
因此,我在运行代码时遇到以下错误:Swift 在CAShapeLayer上绘制bezierPath时上下文无效,swift,calayer,Swift,Calayer,因此,我在运行代码时遇到以下错误: 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
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值: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()
试试这个
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()
}
}
不必调用绘图方法 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()
}
}
不要调用
bezierPath.stroke()
。您正在提供层的路径,而不是绘制路径。不要调用bezierPath.stroke()
。您提供的是图层的路径,而不是绘制路径。