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

标签 swift delegates uiimage uiimagepickercontroller

我目前有View Controller 1,它有UIImageView和一个位于顶部的按钮,当该按钮 > 被点击我对 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)

    }

现在用户选择一个图像,然后我希望能够将该图像放置在用户在 View Controller 1 中按下按钮的原始 UIImageView 中我正在使用的代码崩溃并出现错误 fatal error :在展开可选值时意外发现 nil 使用的代码:

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 传递到下一个 ViewController,我认为它崩溃的原因。

任何人都可以添加任何关于如何执行此操作的输入吗?

谢谢

最佳答案

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

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

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

检查您是否正确传递了 VC1 并且不为零。检查您是否已将 profilePic 连接到 Storyboard中的 socket (如果您正在使用 Storyboard)。检查 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")
    }
}

关于swift - 如何通过动画 UIView 将 UIImage 设置到 SWIFT 中的另一个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28901101/

相关文章:

ios - iOS 中的递归委托(delegate)——如何实现?

animation - iOS5将CGAffineTransform与animationImages结合起来?

ios - 如何使用 Alamofire 将用户个人资料图像保存到 MYSQL 数据库,然后在以下屏幕上检索

ios - swift 错误 : Type of expression is ambiguous without more context

ios - 无法分配给属性 : 'b0' is a get-only property

swift - 无法使用 Alamofire 测试 stub 响应

ios - 如何在填充UIImageViews的NSArray中搜索某个图像属性?

ios - 如何访问连续的两个TextField

c# - 编译器对委托(delegate)的处理

c# - "Uncurrying".NET 中的实例方法