通常情况下,我可以在网上找到这个问题的答案,但是因为它很新,我一直遇到麻烦。
当我让用户登录应用并为他们的个人资料选择4-5张图片时,如何将这些图片存储在Firebase存储中并将这些图片引用到Firebase数据库中的该用户?
由于
答案 0 :(得分:13)
首先将它们上传到 Firebase存储,然后将该网址存储在Firebase数据库中
let storage = FIRStorage.storage()
let data: NSData = myImageData
let userProfilePic = storageRef.child("users/abc/profileimage.jpg")
let uploadTask = userProfilePic.putData(data, metadata: nil) { metadata, error in
if (error != nil) {
// Uh-oh, an error occurred!
} else {
let downloadURL = metadata!.downloadURL
// store downloadURL in db
storeUserProfileInDB(downloadURL)
}
}
func storeUserProfileInDB(profileImgUrl: NSURL) {
let ref = FIRDatabase.database().reference()
let key = ref.child("users").childByAutoId().key
let dictionaryUser = [ "userName" : name! ,
"imageUrl" : profileImgUrl.absoluteString,
]
let childUpdates = ["/users/\(key)": dictionaryTodo]
ref.updateChildValues(childUpdates, withCompletionBlock: { (error, ref) -> Void in
//save
})
}