didFinishPickingMediaWithInfo使用Swift 5时出错

问题描述 投票:2回答:2

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

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

我有另一个项目,我在另一个项目中使用这个确切的方法,工作得非常好。我试图将@objc放在函数前面,但是得到了这个错误:

Objective-C方法由imagePickerController:didFinishPickingMediaWithInfo:方法提供的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)
}

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

ios swift uiimagepickercontroller swift5
2个回答
1
投票

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

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

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

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

0
投票

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

它曾经是[String : Any](这是你有的),但现在他们已经改为[UIImagePickerController.InfoKey : Any]。这种变化只意味着信息将是UIImagePickerController.InfoKey的类型。

尝试使用它作为您的函数名称,它已经改变了一段时间(不是在Swift 5中):

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

希望这可以帮助!

© www.soinside.com 2019 - 2024. All rights reserved.