0

I'm making an app where you upload a photo and then it gets is downloadURL and saves it in a database but im' getting an error before saving the link on the database.

*** Terminating app due to uncaught exception 'FIRInvalidArgumentException', reason: 'Unsupported type: NSURL (found in field Link)'

. . .

libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

THREAD 1: signal SIGABRT

and I can't seem to understand why.

Thanks

    import UIKit
    import Firebase
    var docRefIm: DocumentReference!
    var docRefImC: CollectionReference!

    class carregar: UIViewController,UINavigationControllerDelegate, UIImagePickerControllerDelegate {
        @IBOutlet weak var upload: UIButton!
        @IBOutlet weak var myImage: UIImageView!
        @IBAction func uploadBt(_ sender: Any) {

            let image = UIImagePickerController()
            image.delegate = self
            image.allowsEditing = true
            image.sourceType = UIImagePickerControllerSourceType.photoLibrary
            self.present(image, animated: true){

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

            var selectedImage: UIImage?

            if let editedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
                myImage.image=editedImage
                selectedImage = editedImage
            } else if let image = info[UIImagePickerControllerOriginalImage] as? UIImage
            {
                myImage.image=image
                selectedImage = image
            }
            self.dismiss(animated: true, completion: nil)
            let storageRef = Storage.storage().reference()
            let imageName = NSUUID().uuidString
            let fullImageRef = storageRef.child("casamentoMR/\(imageName).jpeg")
            let compImageRef = storageRef.child("casamentoMR/compressed/\(imageName).jpeg")
            let newMetadata = StorageMetadata()
            newMetadata.contentType = "image/jpeg";
            if let uploadDataComp = UIImageJPEGRepresentation(myImage.image!, 0.15){
                compImageRef.putData(uploadDataComp, metadata: newMetadata, completion: { (metadata, error) in
                    if error != nil {
                        print(error)
                        return
                    } else {
                    compImageRef.downloadURL { (url, error) in
                        guard let downloadURL = url

                            else {
                            // Uh-oh, an error occurred!
                            return
                        }
                        docRefImC = Firestore.firestore().collection("Imagens")
                        docRefImC.getDocuments() { (querySnapshot, err) in
                            if let err = err {
                                print("Error getting documents: \(err)")
                            } else {
                                for document in querySnapshot!.documents {
                                    print("\(document.documentID) => \(document.data())")
                                    id += 1
                                }
                                docRefIm = Firestore.firestore().collection("Imagens/").document(String(id))
                                docRefIm.setData([
                                    "Link": downloadURL,
                                    "ID": id+1
                                ]) { err in
                                    if let err = err {
                                        print("Error writing document: \(err)")
                                    } else {
                                        print("Document successfully written!")
                                    }
                                }
                            }
                        }
                        }}

                })
            }
            if let uploadData = UIImageJPEGRepresentation(myImage.image!, 0.7){
                fullImageRef.putData(uploadData, metadata: newMetadata, completion: { (metadata, error) in
                    if error != nil {
                        print(error)
                        return
                    }                 
                })
            }

        }

        func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
            dismiss(animated: true,completion: nil)
        }
        override func viewDidLoad() {
            super.viewDidLoad()
    view.setGradientBackground(colorOne: Colors.MarPink, colorTwo: Colors.AzRui)
            // Do any additional setup after loading the view.
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
  • 1
    Could you give the whole error message? It should start with "Terminating with" (with an uppercase "t") and until the one you gave? Even if you already answered your question, the error message you gave is missing the important and meaningful part. – Larme Jul 04 '18 at 10:18
  • Yeah, your write. I'm going to update it now – Ricardo Rodrigues Jul 04 '18 at 20:13
  • 1
    You see, with that message it’s saying that the value for the key Link is a NSURL one and it shouldn’t. Quite easy to read/understand and to quick to fix by reading the doc – Larme Jul 04 '18 at 20:23

1 Answers1

0

just add to change the URL to a string by changing

                                "Link": downloadURL,

to

"Link": downloadURL.absoluteString,