【问题标题】:Closing only the ImagePicker and passing image to ScrollViewController仅关闭 ImagePicker 并将图像传递给 ScrollViewController
【发布时间】:2017-01-03 16:31:45
【问题描述】:

我有一个ImagePicker,在出现ScrollViewController 之后显示(1)。

问题:

ImagePicker 中选择图像后,如何关闭 只是 ImagePicker 以便显示 ScrollViewController 然后将选取的图像传递给 ScrollViewController?

(1) 呈现ScrollViewControllerImagePicker

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("ScrollViewControllerID") as! ScrollViewController
self.presentViewController(vc, animated: true) {
    let imagePicker = MyImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = .PhotoLibrary
    imagePicker.allowsEditing = false
    imagePicker.modalTransitionStyle = UIModalTransitionStyle.CoverVertical
    vc.presentViewController(imagePicker, animated: true, completion: nil)
}

关闭后处理ImagePicker

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        let myImage = info[UIImagePickerControllerOriginalImage] as! UIImage

//How to send myImage to the ScrollViewController and close ImagePicker?

}

【问题讨论】:

    标签: ios swift uiviewcontroller uiimagepickercontroller presentviewcontroller


    【解决方案1】:

    MyImagePickerController

    class MyImagePickerController: ... {
    
         var controller:ScrollViewController!
    
         convenience init(controller: ScrollViewController) {
             self.init()
             self.controller = controller
         }
    
    }
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        let myImage = info[UIImagePickerControllerOriginalImage] as! UIImage
    
        controller.imageName = myImage
        self.dismissViewControllerAnimated(true, completion: nil)
    
    }  
    

    这就是你将如何应用它

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("ScrollViewControllerID") as! ScrollViewController
    self.presentViewController(vc, animated: true) {
        let imagePicker = MyImagePickerController(controller: vc)
        imagePicker.delegate = self
        imagePicker.sourceType = .PhotoLibrary
        imagePicker.allowsEditing = false
        imagePicker.modalTransitionStyle = UIModalTransitionStyle.CoverVertical
        vc.presentViewController(imagePicker, animated: true, completion: nil)
    }
    

    【讨论】:

    • 谢谢,我会试试这个。我可以进一步了解这部分到底是做什么的吗? convenience init(controller: ScrollViewController) { self.controller = controller}
    • 它提供了你自己的自定义初始化器,convenience init,它允许你接受你自己的变量。然后使用这些变量,您可以访问其他类的内容。因此,您将前一个控制器作为变量接收,然后您可以使用controller.itemscontroller.myImage 访问前一个控制器的项目。有意义吗?
    • 谢谢@impression7vx 但是,代码的哪一部分消除了ImagePicker,但在屏幕上保留了ScrollViewController
    • 啊,我明白了!所以要做到这一点,你只需在 controller.imageName = myImage 之后执行 self.dismissViewControllerAnimated(true, completion: nil) ,它应该删除 imagepicker 并显示 imagePicker 后面的任何内容,即你的 scrollView
    • 不幸的是,convenience init 给出了两个错误:Use of 'self' in property access 'controller' before self.init initializes selfSelf.init isn't called on all paths before returning from initializerself.dismissViewControllerAnimated(true, completion: nil) 似乎也同时解雇了ImagePickerScrollViewController。但是,如果我删除 imagePicker.delegate = self 只有 ImagePicker 被解雇。我将不得不更深入地研究这一切,看看发生了什么。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-22
    • 1970-01-01
    • 2015-10-12
    • 2013-07-29
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多