Dibuje una línea en time real con Swift 3.0

Estoy tratando de dibujar en un UIImageView. Con Swift 1.2 pude conseguir que funcionara, pero tuve que convertirlo a swift 3.0 y simplemente no puedo hacerlo funcionar.

Lo que necesita hacer es dibujar exactamente lo que dibuja en la pantalla con el dedo.

Los códigos no dan errores, pero simplemente no muestran nada.

Variables;

var lastPoint = CGPoint.zero var networking: CGFloat = 0.0 var green: CGFloat = 0.0 var blue: CGFloat = 0.0 var brushWidth: CGFloat = 10.0 var opacity: CGFloat = 1.0 var swiped = false 

El código;

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { swiped = false if let touch = touches.first { lastPoint = touch.location(in: self.view) } } func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint) { imageView.image?.draw(in: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height)) UIGraphicsBeginImageContext(self.imageView.bounds.size); let context = UIGraphicsGetCurrentContext() context?.move(to: fromPoint) context?.addLine(to: toPoint) context?.setLineCap(CGLineCap.round) context?.setLineWidth(brushWidth) context?.setStrokeColor(networking: networking, green: green, blue: blue, alpha: 1.0) context?.setBlendMode(CGBlendMode.normal) imageView.image = UIGraphicsGetImageFromCurrentImageContext() imageView.alpha = opacity UIGraphicsEndImageContext() } override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { swiped = true if let touch = touches.first { let currentPoint = touch.location(in: view) drawLineFrom(fromPoint: lastPoint, toPoint: currentPoint) lastPoint = currentPoint } } override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { if !swiped { // draw a single point self.drawLineFrom(fromPoint: lastPoint, toPoint: lastPoint) } 

Tienes que comenzar el context de la image:

 UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0) 

También tienes que acariciar la ruta:

 context?.strokePath() 

Tampoco dibujas la image anterior:

 imageView.image?.draw(in: view.bounds) 

Así:

 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(networking: networking, green: green, blue: blue, alpha: 1.0) context?.setBlendMode(CGBlendMode.normal) context?.strokePath() imageView.image = UIGraphicsGetImageFromCurrentImageContext() imageView.alpha = opacity UIGraphicsEndImageContext() }