我显示一个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?

}

最佳答案

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

08-24 22:14