Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

didFinishPickingMediaWithInfo Error using Swift 5

I know this question has been asked before but none of the answers have fixed my issue. I'm getting the error:

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

I have another project where I'm using this exact method in another project that works perfectly fine. I have tried to place @objc in front of the functions but get this error:

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

Thank you for any help fixing this issue.

like image 247
user Avatar asked Mar 28 '19 07:03

user


2 Answers

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)
    }
like image 117
Vishal Vaghasiya Avatar answered Oct 09 '22 13:10

Vishal Vaghasiya


This is not just Swift 5 problem. Type of info parameter for this delegate method was changed from [String : Any] to [UIImagePickerController.InfoKey : Any] a long time ago.

This is why compiler complains that your, in this case your own, method conflicts with method declared by implemented protocol.

So, you need to implement delegate's method with correct type of parameter

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    <#code#>
}
like image 3
Robert Dresler Avatar answered Oct 09 '22 15:10

Robert Dresler