将图像 URL 上传到 Firestore

时间:2021-07-11 08:30:01

标签: ios firebase swiftui

我正在尝试制作像 Tinder 这样的应用程序,我完成了身份验证部分:

      Button(action: {
                                Auth.auth().createUser(withEmail: email, password: password) { (result,error) in
                                    if error != nil{
                                        self.error = error?.localizedDescription ?? ""
                                        self.alert.toggle()
                                    }else{
                                        let db = Firestore.firestore()
                                        let user = Auth.auth().currentUser?.uid
                                    db.collection("Users").document(user ?? "").setData(["Name": name,"Lastname":lastname,"Gender" : gender])
                                        self.next.toggle()
                                        print("success")
                                    }
                                }
                            }, label: {
                                Text("Register")
                                    .bold()
                                    .font(.system(size: 24))
                                    .foregroundColor(Color.black)
                                    .background(
                                        Color.white
                                            .frame(width: 300, height: 60, alignment: .center)
                                            .cornerRadius(20)
                                            .shadow(color: .black, radius: 1, x: 0, y: 5)
                                    )
                            })

但我不知道如何将 photoURL 添加到 Firestore。你们能帮我找到解决方案吗?我对 Firebase 很陌生

感谢评论,我做了一些研究并编写了这段代码。它就像我想要的那样工作。我可以上传图像存储,我可以在 Firestore 中存储 URL。如果有人想使用或尝试学习这里是我的代码:

 func uploadImage(image: UIImage){
    if let imageData = image.jpegData(compressionQuality: 1){
        let storage = Storage.storage()
            storage.reference().child("temp").putData(imageData, metadata: nil){ (data, err) in
            if let err = err{
                print(err.localizedDescription)
            }else{
                print("Image uploaded")
            }
                let url = storage.reference().child("temp")
                url.downloadURL { (url, error) in
                    if error != nil{
                        print("error")
                    }else{
                        self.photoURL = "\(url!)"
                        let db = Firestore.firestore()
                        let user = Auth.auth().currentUser
                        db.collection("Users").document(user!.uid).updateData(["photoURL" : photoURL])
                        
                    }
                }
        }
    }else{
        print("Could not unwrap/case image to data")
    }
}

0 个答案:

没有答案
相关问题