how to draw / doodle line on UIImage in swift?

前端 未结 3 1568
再見小時候
再見小時候 2021-01-01 00:28

I need to draw / doodle a line in UIImage like the picture above , I see a lot of tutorials doodle line on the UIView but not in the UIImage.

after the user

3条回答
  •  抹茶落季
    2021-01-01 01:14

    Details

    • Xcode 10.2.1 (10E1001), Swift 5

    Solution

    import UIKit
    
    // https://stackoverflow.com/a/41009006/4488252
    class DrawnImageView: UIImageView {
        private lazy var path = UIBezierPath()
        private lazy var previousTouchPoint = CGPoint.zero
        private lazy var shapeLayer = CAShapeLayer()
    
        override func awakeFromNib() {
            super.awakeFromNib()
            setupView()
        }
    
        override init(frame: CGRect) {
            super.init(frame: frame)
            setupView()
        }
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
        }
    
        func setupView(){
            layer.addSublayer(shapeLayer)
            shapeLayer.lineWidth = 4
            shapeLayer.strokeColor = UIColor.white.cgColor
            isUserInteractionEnabled = true
        }
    
        override func touchesBegan(_ touches: Set, with event: UIEvent?) {
            super.touchesBegan(touches, with: event)
            if let location = touches.first?.location(in: self) { previousTouchPoint = location }
        }
    
        override func touchesMoved(_ touches: Set, with event: UIEvent?) {
            super.touchesMoved(touches, with: event)
            if let location = touches.first?.location(in: self) {
                path.move(to: location)
                path.addLine(to: previousTouchPoint)
                previousTouchPoint = location
                shapeLayer.path = path.cgPath
            }
        }
    }
    
    // https://stackoverflow.com/a/40953026/4488252
    extension UIView {
        var screenShot: UIImage?  {
            let scale = UIScreen.main.scale
            UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale)
            if let context = UIGraphicsGetCurrentContext() {
                layer.render(in: context)
                let screenshot = UIGraphicsGetImageFromCurrentImageContext()
                UIGraphicsEndImageContext()
                return screenshot
            }
            return nil
        }
    }
    

    Usage

    1. Add DrawnImageView to your root (parent) view (drawing by touch will be enabled automatically)
    2. To save UIImage use drawingImageView.screenShot

    Full sample

    Do not forget to add the solution code here

    import UIKit
    
    class ViewController: UIViewController {
    
        fileprivate weak var savedImageView: UIImageView?
        fileprivate weak var drawnImageView: UIImageView?
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let drawnImageView = addImageView(image: UIImage(named: "swift")) as DrawnImageView
            drawnImageView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
            drawnImageView.heightAnchor.constraint(equalToConstant: UIScreen.main.bounds.height/3).isActive = true
            self.drawnImageView = drawnImageView
    
            let button = UIButton()
            button.setTitle("Save Image", for: .normal)
            button.translatesAutoresizingMaskIntoConstraints = false
            button.setTitleColor(.blue, for: .normal)
            view.addSubview(button)
            button.topAnchor.constraint(equalTo: drawnImageView.bottomAnchor, constant: 60).isActive = true
            button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
            button.heightAnchor.constraint(equalToConstant: 44).isActive = true
            button.addTarget(self, action: #selector(saveImageButtonTouchUpInside), for: .touchUpInside)
    
            let savedImageView = addImageView()
            savedImageView.topAnchor.constraint(equalTo: button.bottomAnchor, constant: 60).isActive = true
            savedImageView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
            self.savedImageView = savedImageView
        }
    
        private func addImageView(image: UIImage? = nil) -> T {
            let imageView = T(frame: .zero)
            imageView.contentMode = .scaleAspectFit
            imageView.image = image
            view.addSubview(imageView)
    
            imageView.translatesAutoresizingMaskIntoConstraints = false
            imageView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
            imageView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
            return imageView
        }
    
        @objc func saveImageButtonTouchUpInside(sender: UIButton) {
            savedImageView?.image = drawnImageView?.screenShot
        }
    }
    

    Results

提交回复
热议问题