如何在swift中自由绘制/涂抹图像上的一行?

时间:2018-01-27 04:45:36

标签: ios swift uiimageview uiimage

我已经搜索了此案例的解决方案,但我没有在互联网上找到它。我想绘制/涂抹像snapchat或whatsapp状态的行,允许用户自由绘制。我找到了一个类似的教程,但它只是在本教程中使用了UIView:https://www.youtube.com/watch?v=gbFHqHHApC4&t=7s

enter image description here

我希望允许用户自由绘制他们从图库/相机中挑选的图像,然后将结果保存为UIImage

enter image description here

UIView上涂鸦的源代码(第一张图片)位于

之下

首先,制作自定义视图

class DrawView : ShadowView {

    var isDrawing = false
    var lastPoint : CGPoint!
    var strokeColor : CGColor! = UIColor.black.cgColor
    var strokes = [Stroke]()


    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        guard !isDrawing else { return }
        isDrawing = true

        guard let touch = touches.first else {return}
        let currentPoint = touch.location(in: self)
        lastPoint = currentPoint
        print(currentPoint)


    }



    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {


        guard isDrawing else { return}

        guard let touch = touches.first else {return}
        let currentPoint = touch.location(in: self)
        let stroke = Stroke(startPoint: lastPoint, endPoint: currentPoint, strokeColor: strokeColor)
        strokes.append(stroke)
        lastPoint = currentPoint

        setNeedsDisplay()


        print(currentPoint)
    }




    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

         guard isDrawing else { return}
         isDrawing = false 

        guard let touch = touches.first else {return}

        let currentPoint = touch.location(in: self)
        let stroke = Stroke(startPoint: lastPoint, endPoint: currentPoint, strokeColor: strokeColor)
        strokes.append(stroke)

        setNeedsDisplay() 

        lastPoint = nil
        print(currentPoint)

    }


    override func draw(_ rect: CGRect) {

        let context = UIGraphicsGetCurrentContext()
        context?.setLineWidth(5) 
        context?.setLineCap(.round) 

        for stroke in strokes {
            context?.beginPath()

            context?.move(to: stroke.startPoint)
            context?.addLine(to: stroke.endPoint)

            context?.setStrokeColor(stroke.strokeColor)
            context?.strokePath()
        }

    }


    func erase() {
        strokes = []
        strokeColor = UIColor.black.cgColor
        setNeedsDisplay() // ditampilkan ke layar
    }



}

斯托克的结构就像这样:

struct  Stroke {
    let startPoint : CGPoint
    let endPoint : CGPoint
    let strokeColor : CGColor

}

之后,将自定义视图分配给ViewController:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var drawView: DrawView!


    override func viewDidLoad() {
        super.viewDidLoad()

    }



    @IBAction func yellowDidPressed(_ sender: Any) {
        drawView.strokeColor = UIColor.yellow.cgColor
    }


    @IBAction func redDidPressed(_ sender: Any) {
         drawView.strokeColor = UIColor.red.cgColor
    }

    @IBAction func blueDidPressed(_ sender: Any) {
         drawView.strokeColor = UIColor.blue.cgColor
    }

    @IBAction func erasedDidPressed(_ sender: Any) {
        drawView.erase()
    }

}

那么如果我想在UIImageView上应用该自定义视图并将结果保存为新的UIImage呢?我实际上是编程的新手,真的需要你的帮助。谢谢:))

0 个答案:

没有答案