如何在视图控制器之间传输 Xcode 8 上的绘图?

How to transfer a drawing on Xcode 8 between view controllers?

我正在尝试制作这个应用程序,我在一个视图控制器上绘制,然后转到另一个视图控制器并在那里绘制相同的图形。我一直在到处寻找,但没有发现任何关于在视图控制器之间传输绘图的信息。我是一名高中生,并且是使用任何编码的初学者,尤其是 Xcode 8 和 Swift 3。如果您能提供任何提示,我将不胜感激。谢谢! :)

更新:所以我在一个视图控制器上的 UIimage 上绘图,并试图将相同的绘图放到另一个视图控制器上,只是为了展示,它在传输后不需要编辑。

这就是我要在 UIimage 上绘制的所有代码

@IBOutlet weak var imageViewPass: UIImageView!

var lastPoint = CGPoint.zero
var swiped = false
var red:CGFloat = 0.0
var green:CGFloat = 0.0
var blue:CGFloat = 0.0

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

    if let touch = touches.first {
        lastPoint = touch.location(in: self.view)
    }
}

func drawlines(fromPoint:CGPoint,toPoint:CGPoint)
{
    UIGraphicsBeginImageContext(self.view.frame.size)
    drawOnIMageFeet.image?.draw(in: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))

    let context = UIGraphicsGetCurrentContext()

    context?.move(to: CGPoint(x: fromPoint.x, y: fromPoint.y))
    context?.addLine(to: CGPoint(x: toPoint.x, y: toPoint.y))

    context?.setBlendMode(CGBlendMode.normal)
    context?.setLineCap(CGLineCap.round)
    context?.setLineWidth(5)
    context?.setStrokeColor(UIColor(red: red, green: green, blue: blue, alpha: 1.0).cgColor)

    context?.strokePath()

    drawOnIMageFeet.image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
}

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

    if let touch = touches.first {
        let currentPoint = touch.location(in: self.view)
        drawlines(fromPoint: lastPoint, toPoint: currentPoint)

        lastPoint = currentPoint
    }
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?)
{
    if !swiped {
        drawlines(fromPoint: lastPoint, toPoint: lastPoint)
    }
}

    @IBAction func pickColor(_ sender: UIButton)
{
    if sender.tag == 0 {
        (red,green,blue) = (1,0,0)
    } else if sender.tag == 1 {
        (red,green,blue) = (0,1,0)
    } else if sender.tag == 2 {
        (red,green,blue) = (0,0,1)
    } else if sender.tag == 3 {
        (red,green,blue) = (1,0,1)
    } else if sender.tag == 4 {
        (red,green,blue) = (1,1,0)
    } else if sender.tag == 5 {
        (red,green,blue) = (0,1,1)
    } else if sender.tag == 6 {
        (red,green,blue) = (0,0,0)
    }
}

使用 segues 将数据传输到另一个视图控制器相对简单。当转换即将发生时,prepare(for:sender:) 函数在当前视图控制器上被调用(查看 docs)。

假设 drawOnIMageFeet 是您当前视图控制器的 属性,该函数对您来说可能看起来像这样:

func prepare(for segue:UIStoryboardSegue, sender:Any?)
{
    // vc will be the view controller that will be displayed after the transition
    if let vc = segue.destination as? MyNextViewController
    {
        // do your transferring of data/setup here
        vc.imageToDraw = drawOnIMageFeet

        // other setup
    }
}

对于来自单个视图控制器的多个 segue,我通常只是根据目标视图控制器 class 来确定要做什么。但是,如果您有多个不同的 segues 从一个视图控制器进入另一个视图控制器,则可以使用 UIStoryboardSegue class 的 identifier 属性 来进一步区分 segues。