仅关闭ImagePicker并将图像传递给ScrollViewController

时间:2016-08-26 23:15:09

标签: ios swift uiviewcontroller uiimagepickercontroller presentviewcontroller

ImagePicker出现后,我显示ScrollViewController

问题:

  

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?

}

1 个答案:

答案 0 :(得分:0)

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