您必须开始图像上下文:
UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0)
您还必须摸索路径:
context?.strokePath()
您也没有绘制上一张图像:
imageView.image?.draw(in: view.bounds)
从而:
func drawLine(from fromPoint: CGPoint, to toPoint: CGPoint) { UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0) imageView.image?.draw(in: view.bounds) let context = UIGraphicsGetCurrentContext() context?.move(to: fromPoint) context?.addLine(to: toPoint) context?.setLineCap(CGLineCap.round) context?.setLineWidth(brushWidth) context?.setStrokeColor(red: red, green: green, blue: blue, alpha: 1.0) context?.setBlendMode(CGBlendMode.normal) context?.strokePath() imageView.image = UIGraphicsGetImageFromCurrentImageContext() imageView.alpha = opacity UIGraphicsEndImageContext()}


