我在ViewController中设置了URLstring变量,但是当我尝试在实时数据库中设置“ profileImageURL”的值时,即使我从storageRef中提取了URL,它也不会显示所需的URL。< / p>
我虽然是因为我已经在方法内部声明了URLString变量(在本例中为Button),但是即使现在我在ViewController中声明了它也不起作用。
这是代码:
Auth.auth().createUser(withEmail: emailTextField.text!, password: passwordTextField.text!) { (user, error) in
if error != nil{
print(error!.localizedDescription)
return
}
let uid = Auth.auth().currentUser?.uid
let storageRef = Storage.storage().reference(forURL: "gs://instagramclone-1ed36.appspot.com").child("Profile_Images").child(uid!)
let ref = Database.database().reference()
if let profileIMG = self.selectedImage, let imageData = profileIMG.jpegData(compressionQuality: 0.1){
storageRef.putData(imageData, metadata: nil, completion: { (metadata, error) in
if error != nil {
return
}
storageRef.downloadURL(completion: { (url, error) in
if error != nil {
return
}else {
self.URLString = url!.absoluteString
print("\n\n the URLString is : \(self.URLString)\n\n")
}
})
let userReference = ref.child("users")
let newUserReference = userReference.child(uid!)
newUserReference.setValue([ "profileImageURL": self.URLString])
})
}
}
答案 0 :(得分:2)
// Fetch the download URL
storageRef.downloadURL { url, error in
if let error = error {
// Handle any errors
if(error != nil){
print(error)
return
}
} else {
// Get the download URL for 'images/stars.jpg'
let urlStr:String = (url?.absoluteString) ?? ""
let values = ["imageUrl": urlStr,"date_time": self.getDate()]
self.registerUserIntoDatabaseWithUID(uid: userID, values: values as [String : AnyObject])
}
}
func registerUserIntoDatabaseWithUID(uid:String, values:[String:AnyObject]){
let ref = Database.database().reference(fromURL: "https://yourpath.firebaseio.com/")
let usersReference = ref.child("abc").child(uid).child("\(self.yourID!)")
usersReference.updateChildValues(values) { (error, ref) in
if(error != nil){
print(error)
return
}
self.dismiss(animated: true, completion: nil)
}
}