didFinishPickingMediaWithInfo 使用 Swift 5 时出错

didFinishPickingMediaWithInfo Error using Swift 5

我知道之前有人问过这个问题,但是 none 的答案已经解决了我的问题。我收到错误:

Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled}

我有另一个项目,我在另一个项目中使用了这个完全正确的方法,并且效果很好。我试图将 @objc 放在函数前面,但出现此错误:

Objective-C method imagePickerController:didFinishPickingMediaWithInfo: provided by method imagePickerController(_:didFinishPickingMediaWithInfo:) conflicts with optional requirement method imagePickerController(_:didFinishPickingMediaWithInfo:) in protocol UIImagePickerControllerDelegate.

  func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    guard let uid = Auth.auth().currentUser?.uid else { return }

    if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage {

        let editImage = editedImage.withRenderingMode(.alwaysOriginal)

        guard let uploadData = editImage.jpegData(compressionQuality: 0.3) else { return }

        let filename = NSUUID().uuidString

        let stoarageRef = Storage.storage().reference().child("profile_images").child(filename)
        stoarageRef.putData(uploadData, metadata: nil) { (metadata, error) in

            if let error = error {
                print("Failed update profile image:", error)
            }

            stoarageRef.downloadURL(completion: { (downloadUrl, error) in
                guard let profileImageUrl = downloadUrl?.absoluteString else { return }

                print("Successfully updated image in storage:", profileImageUrl)

                let dictionaryValues = ["profileImageUrl": profileImageUrl]

                Database.database().reference().child("users").child(uid).updateChildValues(dictionaryValues, withCompletionBlock: { (error, ref) in

                    if let error = error {
                        print("There was an error:", error)
                        return
                    }

                    print("Successfully saved user info to db")

                })
            })
        }
    } else if let originalImage = info["UIImagePickerControllerOriginalImage"] as? UIImage {

        let origImage = originalImage.withRenderingMode(.alwaysOriginal)

        guard let uploadData = origImage.jpegData(compressionQuality: 0.3) else { return }

        let filename = NSUUID().uuidString

        let stoarageRef = Storage.storage().reference().child("profile_images").child(filename)
        stoarageRef.putData(uploadData, metadata: nil) { (metadata, error) in

            if let error = error {
                print("Failed update profile image:", error)
            }

            stoarageRef.downloadURL(completion: { (downloadUrl, error) in
                guard let profileImageUrl = downloadUrl?.absoluteString else { return }

                print("Successfully updated image in storage:", profileImageUrl)

                let dictionaryValues = ["profileImageUrl": profileImageUrl]

                Database.database().reference().child("users").child(uid).updateChildValues(dictionaryValues, withCompletionBlock: { (error, ref) in

                    if let error = error {
                        print("There was an error:", error)
                        return
                    }

                    print("Successfully saved user info to db")

                })
            })
        }
    }

    dismiss(animated: true, completion: nil)
}

感谢您帮助解决此问题。

新函数名称是导致错误的原因,它与两个执行相同操作的不同 函数混淆。

它曾经是 [String : Any](这是你拥有的)但现在他们已将其更改为 [UIImagePickerController.InfoKey : Any]。此更改仅意味着信息将成为 UIImagePickerController.InfoKey.

类型

尝试使用这个作为你的函数名称,它在不久前被更改了(不在 Swift 5 中):

@objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])

希望对您有所帮助!

这不仅仅是Swift5个问题。此委托方法的 info 参数类型已从 [String : Any] 更改为 [UIImagePickerController.InfoKey : Any] 很久以前。

这就是编译器抱怨您的方法与已实现协议声明的方法冲突的原因,在本例中是您的自己的

因此,您需要使用正确的参数类型来实现委托的方法

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    <#code#>
}

Swift5,Xcode11

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
            backgroundImage = (info[UIImagePickerController.InfoKey.originalImage] as? UIImage)!
            ivBackground.image = backgroundImage

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