我知道这是因为不再使用StorageMetadata。我看到了类似的问题的其他一般性答案,指向此Firebase文档。:https://firebase.google.com/docs/storage/ios/upload-files我尝试将其应用于我的代码,但不起作用。我应该如何将其应用于当前功能?
{
let uid = Auth.auth().currentUser!.uid
let newPostRef = Database.database().reference().child("people").child(uid).child("PhotoPosts")
let newPostRef1 = Database.database().reference().child("people").child(uid).child("PhotoPosts1")
let newPostKey = newPostRef.key
if let imageData = image.jpegData(compressionQuality: 0.001){
let imageStorageRef = Storage.storage().reference().child("images").child(uid)
let newImageRef = imageStorageRef.child(newPostRef.key!)
let newImageRef1 = imageStorageRef.child(newPostRef1.key!)
newImageRef.putData(imageData).observe(.success, handler: {(snapshot) in
self.imageDownloadURL = snapshot.metadata?.downloadURL()?.absoluteString
newPostRef.setValue(self.imageDownloadURL as Any)
})
newImageRef1.putData(imageData).observe(.success, handler: {(snapshot) in
self.imageDownloadURL = snapshot.metadata?.downloadURL()?.absoluteString
let keyToPost = Database.database().reference().child("people").child(uid).childByAutoId().key
let f1: [String: Any] = [(keyToPost) : self.imageDownloadURL as Any]
newPostRef1.updateChildValues(f1)
})
let caption = ServerValue.timestamp() as! [String : Any]
Database.database().reference().child("people").child(uid).child("caption").setValue(caption)
}
}
就像我提到的那样,我尝试将firebase文档应用于函数,如下所示。它仍然给出基本相同的错误。我要去哪里错了?
func save() {
let uid = Auth.auth().currentUser!.uid
let newPostRef = Database.database().reference().child("people").child(uid).child("PhotoPosts")
let newPostRef1 = Database.database().reference().child("people").child(uid).child("PhotoPosts1")
let newPostKey = newPostRef.key
if let imageData = image.jpegData(compressionQuality: 0.001){
let imageStorageRef = Storage.storage().reference().child("images").child(uid)
let newImageRef = imageStorageRef.child(newPostRef.key!)
let newImageRef1 = imageStorageRef.child(newPostRef1.key!)
newImageRef.downloadURL { url, error in
if let error = error {
} else {
newImageRef.putData(imageData).observe(.success, handler: {(snapshot) in
self.imageDownloadURL = snapshot.metadata?.downloadURL()?.absoluteString
newPostRef.setValue(self.imageDownloadURL as Any)
})
// Here you can get the download URL for 'simpleImage.jpg'
}
}
....
您得到:
类型“ StorageMetadata”的值没有成员“ downloadURL”
[如果您查看documentation for StorageMetadata
,您会发现它确实没有StorageMetadata
成员。该成员已删除2018年5月的SDK更新,因此早已消失。
downloadURL
的文档中显示了获取下载URL的正确方法:
uploading data
在您的情况下,类似于:
// Upload the file to the path "images/rivers.jpg"
let uploadTask = riversRef.putData(data, metadata: nil) { (metadata, error) in
guard let metadata = metadata else {
// Uh-oh, an error occurred!
return
}
// Metadata contains file metadata such as size, content-type.
let size = metadata.size
// You can also access to download URL after upload.
riversRef.downloadURL { (url, error) in
guard let downloadURL = url else {
// Uh-oh, an error occurred!
return
}
}
}