问题
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 theImagePicker
so that theScrollViewController
is displayed and then pass the image picked to theScrollViewController
?
(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