Closing only the ImagePicker and passing image to ScrollViewController

瘦欲@ 提交于 2020-01-07 02:16:32

问题


I have an ImagePicker that is displayed after a ScrollViewController is presented (1).

Question:

Once the image is picked from the ImagePicker, how do I dismiss just the ImagePicker so that the ScrollViewController is displayed and then pass the image picked to the ScrollViewController?

(1) Presenting ScrollViewController and ImagePicker

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)
}

Handling ImagePicker after it is closed

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

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

}

回答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)

}  

This is how you would apply it

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)
}


来源:https://stackoverflow.com/questions/39175823/closing-only-the-imagepicker-and-passing-image-to-scrollviewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!