代码之家  ›  专栏  ›  技术社区  ›  Gorib Developer

如何使用swift 4通过ImagePicker控制器发送imagedata

  •  1
  • Gorib Developer  · 技术社区  · 6 年前

    enter image description here 我有 tabbarController CameraViewControlle 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 回复  |  直到 6 年前
        1
  •  1
  •   Kuldeep Jayesh Lathiya    6 年前

    你的 doneButtonDidPress 方法在中实现 TabBarController 并且您希望选中 Images 在里面 CameraVC 所以做一件事添加 NotificationCenter 在里面 CameraVC公司 并将该通知发布到 图像 array 一旦你完成了 Image 选择并选择加载 images 从那以后 大堆 在里面 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]
    }
    

    UITABBARC控制器

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