Change your custom func to:
func draw(inContext context: CGContext, rect: CGRect, bkgColor: UIColor?) {
// if a background color was passed, fill the background
if let c = bkgColor {
context.setFillColor(c.cgColor)
context.fill(rect)
}
// 2
context.setLineWidth(5)
context.setStrokeColor(UIColor.black.cgColor)
context.setLineCap(.round)
//context.setFillColor(UIColor.white.cgColor)
// 3
for line in lineArray {
// 4
guard let firstPoint = line.first else { continue }
context.beginPath()
context.move(to: firstPoint)
// 5
for point in line.dropFirst() {
context.addLine(to: point)
}
context.strokePath()
}
}
and change your calls:
override func draw(_ rect: CGRect) {
guard let context = UIGraphicsGetCurrentContext() else { return }
//draw(inContext: context)
draw(inContext: context, rect: rect, bkgColor: nil)
}
and:
func exportDrawing() -> UIImage? {
// 2
UIGraphicsBeginImageContext(frame.size)
guard let context = UIGraphicsGetCurrentContext() else { return nil }
// 3
//draw(inContext: context)
draw(inContext: context, rect: CGRect(origin: .zero, size: frame.size), bkgColor: .white)
// 4
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…