Reference Firebase users in database with images in Firebase Storage Swift

前端 未结 1 1963
庸人自扰
庸人自扰 2021-02-05 18:08

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

When I have users sign into the app and they choose 4

1条回答
  •  礼貌的吻别
    2021-02-05 18:55

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

    0 讨论(0)
提交回复
热议问题