如何通过动画 UIView 将 UIImage 设置为 SWIFT 中的另一个 View Controller

How do I set an UIImage through a animated UIView to another View Controller in SWIFT

我目前有 View Controller 1,其中有 UIImageView 和一个 button 放在顶部,当 button 被点击时,我制作一个 PopUpViewController 动画这基本上是带有按钮的 UIView,它在 View Controller 1

的顶部进行动画处理
var popViewController : PopUpViewControllerSwift! //popViewController declared in View Controller 1

 @IBAction func changePicture(sender: UIButton) {


            self.popViewController = PopUpViewControllerSwift(nibName: "PopUpViewController_iPhone6", bundle: nil)
            self.popViewController.showInView(self.view, animated: true)
    }

popUpViewController 现在在 ViewController 1 上弹出,然后用户按下一个按钮并显示 ImagePickerController 照片库。代码在这里:

@IBAction func goToPhotoLibary(sender: UIButton) {

        self.removeAnimate()
        var image = UIImagePickerController()
        image.delegate = self
        image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        image.allowsEditing = true
        presentViewController(image, animated: true, completion: nil)

    }

现在用户选择了一张图片,然后我希望能够将该图片放在原始 UIImageView 中,用户用 View Controller 1 中的按钮按下我正在使用的代码是因使用错误 fatal error: unexpectedly found nil while unwrapping an Optional value 代码而崩溃:

var VC1 : ProfileViewController! //Reference to View Controller 1

    func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage imagePicked: UIImage!, editingInfo: [NSObject : AnyObject]!) {

            self.dismissViewControllerAnimated(true, completion: nil)
             self.VC1.profilePic.image = imagePicked //CRASHES HERE

}

我正在尝试将从一个 ViewController 中选取的图像传递到下一个,我认为它为什么会崩溃。

任何人都可以在如何执行此操作中添加任何输入吗?

谢谢

首先我会尝试设置您的图像,然后关闭控制器。

func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage imagePicked: UIImage!, editingInfo: [NSObject : AnyObject]!) {
    self.VC1.profilePic.image = imagePicked //CRASHES HERE
    self.dismissViewControllerAnimated(true, completion: nil)
}

现在你需要做的是检查哪个变量为nil。

检查您是否正确传递了 VC1,并且不是 nil。检查您是否已将 profilePic 连接到故事板中的插座(如果您使用的是故事板)。检查 imagePicked 是否不为零。

您可以通过以下方式完成:

if let value = imagePicked{
    println("image is not nil")
}
if let value = self.vc1{
    println("vc1 is not nil")
    if let profilepic = self.vc1.profilePic{
        println("profile pic is not nil")
    }
}