gpt4 book ai didi

swift - 如何从 firebase 存储中获取 imageUrl 并存储在 Xcode 10 swift 4.2 中的 firebase 数据库中

转载 作者:行者123 更新时间:2023-11-28 14:08:46 33 4
gpt4 key购买 nike

这是我正在使用的代码

@objc func handleSignUp() {
guard let email = emailTextField.text, email.characters.count > 0 else { return }
guard let username = usernameTextField.text, username.characters.count > 0 else { return }
guard let password = passwordTextField.text, password.characters.count > 0 else { return }

Auth.auth().createUser(withEmail: email, password: password, completion: { (user, error) in
if let err = error {
print("Failed to create user:", err)
return
}
print("Successfully created user:", user?.user.uid)
// guard let image = self.plusPhotoButton.imageView?.image else { return }
// guard let uploadData = UIImageJPEGRepresentation(image, 0.3) else { return }
guard let uploadData = self.plusPhotoButton.imageView?.image?.jpegData(compressionQuality: 0.3) else{return}

let filename = NSUUID().uuidString
Storage.storage().reference().child("profile_images").child(filename).putData(uploadData, metadata: nil, completion: { (metadata, err) in
if let err = err {
print("Failed to upload profile image:", err)
return
}
//this code isn't correct for swift4.2
//guard let profileImageUrl = metadata?.downloadURL()?.absoluteString else { return }


func getDownloadURL(from path: String, completion: @escaping (URL?, Error?) -> Void) {
let storageReference = Storage.storage().reference(forURL: "gs://instagramfirebase-60be5.appspot.com")

let storageRef = storageReference.child(path).downloadURL(completion: completion)
}
print("Successfully uploaded profile image")//,profileImageUrl

guard let uid = user?.user.uid else { return }
let dictionaryValues = ["username": username] as [String : Any]//, "profileImageUrl": profileImageUrl
let values = [uid: dictionaryValues]

Database.database().reference().child("users").updateChildValues(values, withCompletionBlock: { (err, ref) in

if let err = err {
print("Failed to save user info into db:", err)
return
}

print("Successfully saved user info to db")
})
})
})
}

截图如下:

我对如何从 firebase 存储中获取 imageUrl 并将其存储在 Xcode 10 swift 4.2 的 firebase 数据库中感到困惑

请帮忙,谢谢你的时间。

最佳答案

您可以通过在图像上传的闭包中使用以下代码轻松获取下载 url:

storageRef.downloadURL(completion: { (url, error) in
if let err = error{
// error happened - implement code to handle it
print(err)
} else {
// no error happened; so just continue with your code
print(url?.absoluteString) // this is the actual download url - the absolute string
}

请注意:不要单独存储 downloadURL,因为 Firebase 可以更改 downloadURL 的标记,我建议始终掌握完整的存储路径。

希望我能帮上忙。

关于swift - 如何从 firebase 存储中获取 imageUrl 并存储在 Xcode 10 swift 4.2 中的 firebase 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52754028/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com