如何将录音和图像同时上传到Firebase存储

时间:2019-03-15 19:57:31

标签: ios swift firebase firebase-realtime-database firebase-storage

我正在尝试允许用户上传图像以及他们的录音。根据Firebase文档,我将数​​据库引用嵌套在存储引用中以上传它们,但是无法创建和发送图像和音频的URL。我可以将它们分别作为两个不同的数据库引用发送,但是将它们下载回应用程序时会出现问题(没有某种类型的标识符链接图像和音频)。如果有人知道如何将两者作为一个参考发送,我们将为您提供帮助。

func uploadRecording () {
    if let audioRecording = recordedAudioURL {
        let audioIDString = NSUUID().uuidString
        let audioStorageRef = Storage.storage().reference(forURL: Config.STORAGE_ROOT_REFERENCE).child("posts").child(audioIDString)

        audioStorageRef.putFile(from: audioRecording, metadata: nil, completion: {(_ , error: Error?) in
            if error != nil {
                print (error)
                return
            }
            audioStorageRef.downloadURL(completion: { (url: URL?, error: Error?) in
                if let audioDownloadURL = url?.absoluteString {
                    if error != nil {
                        print(error)
                        return
                    }
                   sendDatatoDatabase(recordedAudioURL: audioDownloadURL)
                }
            })
        })
    }
    // upload selected image to Firebase
    func uploadImage () {
        if let lessonImg = self.selectedImage, let imageData = lessonImg.jpegData(compressionQuality: 0.1) {
            let photoIDString = NSUUID().uuidString
            let photoStorageRef = Storage.storage().reference(forURL: Config.STORAGE_ROOT_REFERENCE).child("posts").child(photoIDString)

            photoStorageRef.putData(imageData, metadata: nil, completion: {(_ , error: Error?) in
                if error != nil {
                    print(error)
                    return
                }
                photoStorageRef.downloadURL(completion: { (url: URL?, error: Error?) in
                    if let photoDownloadURL = url?.absoluteString {
                        if error != nil {
                            print(error)
                            return
                        }
                    }
                })
            }
        )}
    }

对于newPostReference,我收到一个错误:使用未解析的标识符'photoDownloadURL'

func sendDatatoDatabase (recordedAudioURL: String) {
        let ref = Database.database().reference()
        let postsReference = ref.child("posts")
        let newPostID = postsReference.childByAutoId().key
        let newPostReference = postsReference.child(newPostID!)
        newPostReference.setValue(["recordingURL": recordedAudioURL, "photoURL": photoDownloadURL, "lessonTitle": lessonTitleTextField.text!, "lessonTopic": lessonTopicTextField.text!, "lessonLocation": lessonlocationTextField.text!])
        self.lessonlocationTextField.text = " "
        self.lessonTopicTextField.text = " "
        self.lessonTitleTextField.text = " "
        self.tabBarController?.selectedIndex = 1
    }

0 个答案:

没有答案