如何根据用户ID存储图片上传

时间:2018-12-06 07:28:46

标签: ios swift firebase firebase-realtime-database firebase-storage

func uploadImage(){

    let storage = Storage.storage()
    let storageRef = storage.reference()
    let uploadData = self.imageView.image!.jpegData(compressionQuality: 0.75)
    let imagesRef = storageRef.child("images/myImage.jpg") //not sure how is it done
    let uploadTask = imagesRef.putData(uploadData!, metadata: nil) { (metadata, error) in
        guard let metadata = metadata else {
            return
        }

        let size = metadata.size

        imagesRef.downloadURL { (url, error) in
            guard let downloadURL = url else {
                return
            }
        }
    }
}


func retrieveData(){

    let userID = Auth.auth().currentUser?.uid
    ref.child("users").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in

        let userData = snapshot.value as? NSDictionary
        print("Welcome back,", userData?["username"])

    }) { (error) in
        print(error.localizedDescription)
    }
}

您好,我正在寻找一种方法,供用户根据其用户ID将图像上传到Firebase。有人可以建议如何实现吗?预先感谢。

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

func sendMessageWithProperties(properties: [String: Any]) {
    let ref = Database.database().reference().child("messages")
    let childRef = ref.childByAutoId()

    let toId = self.user?.id
    let fromId = Auth.auth().currentUser?.uid
    let timestamp: Int = Int(NSDate().timeIntervalSince1970)
    var values: [String: Any] = ["toId": toId!,
                                 "fromId": fromId!,
                                 "timestamp": timestamp]

    // To append other properties into values
    properties.forEach({values[$0] = $1})

    childRef.updateChildValues(values) { (error, ref) in
        if error != nil {
            print(error!)
            return
        }

        let userMessageRef = Database.database().reference().child("user-messages").child(fromId!).child(toId!)
        let messageId = childRef.key
        userMessageRef.updateChildValues([messageId: 1])

        let receiverMessageRef = Database.database().reference().child("user-messages").child(toId!).child(fromId!)
        receiverMessageRef.updateChildValues([messageId: 1])
    }
}