从 collectionViewCell 呈现 UIImagePickerController class

present UIImagePickerController from collectionViewCell class

我在显示 collectionViewCell 中的 UIImagePicker 时遇到了一些问题。 I know collectionViewCells do not act as the picker delegate by default so I did try to call a method from the UIViewController class operating the cell holding the imageView tapped.我的控制台中没有显示任何错误,但是当我 运行 应用程序并尝试显示 UIImagePickerController 时,没有任何反应,我收到 "Warning: Attempt to present on whose view is not in the window hierarchy!"。我想在用户点击 profileImageView 时显示 UIImagePickerController。在此先感谢您的帮助!

// LoginCell class持有imageView从LoginController调用UIImagePickerController方法class

       lazy var profileImageView: UIImageView = {
            let imageView = UIImageView()
         imageView.addGestureRecognizer(UITapGestureRecognizer(target:self, action: #selector(handleSelectProfileImage)))
            imageView.isUserInteractionEnabled = true
            return imageView
        }() 

        var loginController: LoginController?

        func handleSelectProfileImage() {
        let loginController = LoginController()
        loginController.showImagePicker(sendingVC: loginController)
    }

// LoginController class 作为 UIImagePickerController 委托

    func showImagePicker(sendingVC: LoginController) {
        let picker = UIImagePickerController()

        picker.delegate = self
        picker.allowsEditing = true

        present(picker, animated: true, completion: nil)

    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let loginCell = collectionView.dequeueReusableCell(withReuseIdentifier: loginCellId, for: indexPath) as! LoginCell
            loginCell.delegate = self
            return loginCell
 }

试试这个,在你的手机里:

func handleSelectProfileImage() {

    guard let loginController = delegate as? LoginViewController else {
        return
    }

    loginController.showImagePicker()
}

在您的登录控制器中

func showImagePicker() {

    let picker = UIImagePickerController()

    picker.delegate = self
    picker.allowsEditing = true

    present(picker, animated: true, completion: nil)
}