使用Firebase存储Swift中的图像引用数据库中的Firebase用户 [英] Reference Firebase users in database with images in Firebase Storage Swift

查看:134
本文介绍了使用Firebase存储Swift中的图像引用数据库中的Firebase用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常情况下,我可以在网上找到这个问题的答案,但是因为它很新我遇到了麻烦。

Normally I would be able to find an answer to this question online but since its so new I have been having trouble.

当我有用户登录应用程序时并且他们为自己的个人资料选择4-5张图片,如何将这些图片存储在Firebase存储中并将这些图像引用到Firebase数据库中的该用户?

When I have users sign into the app and they choose 4-5 pictures for their profile, how do I store those images in Firebase Storage and reference those images to that user in Firebase Database?

谢谢

推荐答案

您将它们上传到 Firebase首先存储,然后将网址存储在Firebase数据库中

You upload them to the Firebase Storage first and then store the url in Firebase Database

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
    })

}

这篇关于使用Firebase存储Swift中的图像引用数据库中的Firebase用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆