无法识别StorageMetadata //“StorageMetadata”类型的值没有成员'downloadURL'

时间:2018-05-14 14:22:02

标签: swift firebase metadata

我在更新Firebase项目后收到错误消息类型'StorageMetadata'的值没有成员'downloadURL'。我在最后几天没有改变任何代码,随机它不​​再工作了。 相同的错误出现在3个不同的位置:

1:

// Referencing Firebase storage child with the unique identifier, and updating with the image from the picker
        DatabaseManager.shared.REF_POSTS_IMAGES.child(imgUid).putData(imgData, metadata: metaData, completion: { (metadata, error) in
            if error != nil {
                print("Unable to upload image Firebase storage")
            } else {
                print("Successfully uploaded image to Firebase storage")
                let downloadUrl = metadata?.downloadURL()?.absoluteString
                if let url = downloadUrl {
                    //once the image is uploaded to firebase stoarge, its then posted to the database
                    self.postToFirebase(imgUrl: url)
                }
            }
        })

2:

func uploadImages(_ data:Data,userid:String){

    let metadata = StorageMetadata()
    metadata.contentType = "image/jpeg"

    let imagesRef = Storage.storage().reference().child("photoPosts")
    let childs = imagesRef.child(userid)
    var imgUrl = NSURL()

    childs.putData(data, metadata: metadata) { (metadata, error) in
        imgUrl = (metadata?.downloadURL())! as NSURL

        self.updateURL(userid: userid, user_URL: imgUrl)
    }
}

3:

func save() {
    let newPostRef = Database.database().reference().child("posts").childByAutoId()
    let newPostKey = newPostRef.key

    // 1. save image
    if let imageData = UIImageJPEGRepresentation(image, 0.5) {
        let storage = Storage.storage().reference().child("images/\(newPostKey)")

        storage.putData(imageData).observe(.success, handler: { (snapshot) in
            self.downloadURL = snapshot.metadata?.downloadURL()?.absoluteString
            let postDictionary = [
                "imageDownloadURL" : self.downloadURL,
                "caption" : self.caption
            ]
            newPostRef.setValue(postDictionary)
        })
    }
}

其他帖子的答案对我没有帮助

2 个答案:

答案 0 :(得分:0)

随着swift 5.0的更改,元数据不再具有downloadURL方法。

相反,您必须执行以下操作:

let storageRef = Storage.storage().reference().child("profile_images").child(fileName)
    storageRef.downloadURL(completion: { (url, error) in
    if error != nil {
        print("Failed to download url:", error!)
        return
    } else {
       //Do something with url
      }

  })
})

希望这会有所帮助。您还可以查看以下内容:thisthisthis答案,或仅查看文档here

答案 1 :(得分:0)

我通过上传视频来完成相同的任务,如下所示: 希望对您有所帮助:

 let storageRef = Storage.storage().reference().child("Videos").child("name")
            if let uploadData = self.finalCompressedData as Data? {
                storageRef.putData(self.finalCompressedData, metadata: nil
                    , completion: { (metadata, error) in
                        if let error = error {
                            //                            failure(error)
                        }else{
                            storageRef.downloadURL { (url, error) in
                                guard let downloadURL = url else {
                                    // An error occurred!
                                    return
                                }
                                print(downloadURL)
                            }

                        }
                })
            }