5

我知道以前有人问过这个问题,但没有一个答案能解决我的问题。我收到错误:

错误域=PlugInKit 代码=13 “查询已取消” UserInfo={NSLocalizedDescription=查询已取消}

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

imagePickerController:didFinishPickingMediaWithInfo:方法提供的Objective-C 方法与协议中imagePickerController(_:didFinishPickingMediaWithInfo:)的可选需求方法冲突。imagePickerController(_:didFinishPickingMediaWithInfo:)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)
}

感谢您为解决此问题提供的任何帮助。

4

3 回答 3

9

斯威夫特 5,Xcode 11

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)
    }
于 2019-11-12T11:57:29.687 回答
3

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

这就是为什么编译器抱怨你的,在这种情况下是你自己的,方法与实现的协议声明的方法冲突。

所以,你需要用正确的参数类型来实现委托的方法

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    <#code#>
}
于 2019-03-28T08:16:07.633 回答
2

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

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

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

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

希望这可以帮助!

于 2019-03-28T08:05:52.860 回答