Search code examples
iosuitabbarcontrolleruiimagepickercontrollerswift4

How to sent imagedata with ImagePicker Controller using swift 4


enter image description hereI have tabbarController and CameraViewController. I want to show the camera after clicking on camera tab. I have done it. after open the camera i want to show that particular image selected to the next viewController, but unable to do it. i am using UIImagePickerController, please help

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
    let tabBarIndex = tabBarController.selectedIndex
    if tabBarIndex == 0 {
        //do your stuff
        print("First Tab")
    } else if tabBarIndex == 1 {
        print("Second Tab")
    } else if tabBarIndex == 2 {
        //do the camera stuff here
        let imagePickerController1 = ImagePickerController()
        imagePickerController1.delegate = self
        imagePickerController1.imageLimit = 2
        present(imagePickerController1,animated: true,completion: nil)
        print("camera")
        print("Third Tab")
    }     
}

Done button pressed code.

func doneButtonDidPress(_ imagePicker: ImagePickerController, images: [UIImage]) {
    let firstVC = self.storyboard!.instantiateViewController(withIdentifier: "CameraVC") as! CameraVC

    show(firstVC, sender: nil)   
    dismiss(animated: true, completion: nil)
    print("done")
}



func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let image = info["UIImagePickerControllerOriginalImage"] as? UIImage {

    }
    dismiss(animated: true, completion: nil)
}

Solution

  • Your doneButtonDidPress method is implemented in TabBarController and you want selected Images in CameraVC so do 1 thing add NotificationCenter in CameraVC and Post that notification with Images array once you done with Image pick. and load selected images from that array in CameraVC.

    CameraVC

    NotificationCenter.default.addObserver(self, selector: #selector(updateSelectedImages(_:)), name: NSNotification.Name(rawValue: "updateSelectedImages"), object: nil)
    
    
    @objc func updateSelectedImages(_ notification: Notification) {
        let imagesInfo = notification.object as? NSDictionary
    
        self.arrImages = imagesInfo?.value(forKey: "selectedIamges") as! [UIImage]
        self.imageView.image = self.arrImages[0]
        self.imageView1.image = self.arrImages[1]
    }
    

    UITabBarController

    let dict = NSMutableDictionary()
    dict.setValue(images, forKey: "selectedIamges")
    NotificationCenter.default.post(name: NSNotification.Name("updateSelectedImages"), object: dict)