如何使用swift 4使用ImagePicker Controller发送imagedata

时间:2018-04-24 10:25:40

标签: ios uitabbarcontroller uiimagepickercontroller swift4

enter image description here我有tabbarControllerCameraViewControlle r。我想在点击相机标签后显示相机。我已经做了。打开相机后,我想显示选择的特定图像到下一个viewController,但无法执行此操作。我正在使用UIImagePickerController,请帮助

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

完成按钮代码。

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

1 个答案:

答案 0 :(得分:1)

您的doneButtonDidPress方法已在TabBarController中实施,并且您希望在Images中选择CameraVC,因此在NotificationCenter中添加CameraVC即可完成Images选择后,使用array Image发布该通知。并从images中的array加载选定的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)