使用Swift 5的didFinishPickingMediaWithInfo错误

时间:2019-03-28 07:55:05

标签: ios swift uiimagepickercontroller swift5

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

  

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

我有另一个项目,在另一个工作得很好的项目中正在使用此确切方法。我试图将@objc放在函数前面,但出现此错误:

  

方法imagePickerController:didFinishPickingMediaWithInfo:提供的客观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)
}

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

3 个答案:

答案 0 :(得分:5)

Swift 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)
    }

答案 1 :(得分:0)

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

它曾经是[String : Any](您拥有的),但是现在他们将其更改为[UIImagePickerController.InfoKey : Any]。此更改仅表示该信息将为UIImagePickerController.InfoKey类型。

尝试使用它作为函数名,它在不久前已更改(在Swift 5中不是):

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

希望这会有所帮助!

答案 2 :(得分:0)

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

这就是为什么编译器会抱怨您的拥有方法与已实现协议声明的方法冲突的原因。

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

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