Search code examples
swiftcgpointcgpathuigraphicscontexttouchesended

line disagreeing when touch end when drawing line


My swift code goal is to just draw lines on a image view. Right now when you pick up your hand from the image view the drawing disapers. When your hand is pressed down it works its just when your hand or point is released nothing is saved. I don't know what I am missing in my code to fix this it might just be one line.

import UIKit
class ViewController: UIViewController {
    
    var statPoint = CGPoint.zero
    var swipe = false
    var pic = UIImageView()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        pic.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(pic)
        pic.backgroundColor = .brown
        view.backgroundColor = .cyan
        pic.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let touch = touches.first else {
            return}
        
        swipe = false
        statPoint = touch.location(in: view)
    }
    
    func drawLine(from fromPoint: CGPoint, to toPoint : CGPoint)  {
        UIGraphicsBeginImageContext( view.frame.size)
        
        guard let context = UIGraphicsGetCurrentContext() else {
            return
        }
        
        pic.image?.draw(in: view.bounds)
        context.move(to: fromPoint)
        context.addLine(to: toPoint)
        context.setLineCap(.round)
        context.setLineWidth(5)
        context.setBlendMode(.normal)
        context.setStrokeColor(UIColor.black.cgColor)
        context.strokePath()
        pic.image = UIGraphicsGetImageFromCurrentImageContext()
        pic.alpha = 1
        
        UIGraphicsEndImageContext()
        
        
        
    }
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let touch = touches.first else {
            return
            
        }
        swipe = true
        let currentPoint = touch.location(in: view)
        drawLine(from: statPoint, to: currentPoint)
        statPoint = currentPoint
    }
    
    
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        if !swipe {
            drawLine(from: statPoint, to: statPoint)
        }
        
        UIGraphicsBeginImageContext(pic.frame.size)
        pic.image?.draw(in: view.bounds, blendMode: .normal, alpha: 1)
        pic.image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    }
}

Solution

  • I have checked your code. I think issue is with the frame you're using in touchedEnded.

    Replace your touchedEnded with below code

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
       if !swipe {
           drawLine(from: statPoint, to: statPoint)
       }
           
       UIGraphicsBeginImageContext(view.frame.size)
       pic.image?.draw(in: view.bounds, blendMode: .normal, alpha: 1)
       pic.image = UIGraphicsGetImageFromCurrentImageContext()
       UIGraphicsEndImageContext()
    }