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

时间:2015-03-06 14:38:56

标签: swift delegates uiimage uiimagepickercontroller

我目前有View Controller 1 UIImageViewbutton位于顶部,当点击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

}

我正在尝试将imagePicked从一个ViewController传递到下一个,我假设它为什么会崩溃。

任何人都可以添加任何输入来解决这个问题吗?

由于

1 个答案:

答案 0 :(得分:1)

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

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

现在您需要做的是检查哪个变量为零。

检查您是否正确传递了VC1并且不是nil。检查您是否在storyboard中将profilePic连接到outlet(如果您使用的是故事板)。检查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")
    }
}