除从fireBase显示的图像之外的所有数据



我正在开发一个应用程序,用户创建一个帐户并将图像上传到Firebase数据库,然后图像显示在个人资料页面上。它似乎将图像存储在数据库中,但配置文件页面没有检索图像以显示它。它将所有其他信息传递到页面(电子邮件、用户名等(,但不传递个人资料图片。

这是用于获取要在配置文件页面上显示的数据的代码:

if let user = DataService.dataService.currentUser {
username.text = user.displayName
email.text = user.email
if user.photoURL != nil {
if let data = NSData(contentsOf: user.photoURL!){
self.profileimage!.image = UIImage.init(data: data as Data)
}
}
}
else {
// No user is signed in
}

以下是将图像存储到 Firebase 中的代码:

let filepath = "profileimage/(String(describing: Auth.auth().currentUser!.uid))"
let metadata = FirebaseStorage.StorageMetadata()
metadata.contentType = "image/jpeg"
self.storageRef.child(filepath).putData(data as Data, metadata: metadata, completion: {(metadata, error) in
if let error = error {
print ("(error.localizedDescription)")
return
}
)

提前感谢!

NSData(contentOf:_( 应该只用于本地文件路径,而不是基于网络的 URL。 在此处阅读更多原因:https://developer.apple.com/documentation/foundation/nsdata/1413892-init

我通常在UIImageView上创建一个扩展程序来从URL加载图像,下面是一个例子:

extension UIImageView
{
func loadImageUsingUrlString(_ urlString: String) {
self.image = nil
guard let url = URL(string: urlString) else { return }
URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) in
if let error = error {
print(error)
return
}
DispatchQueue.main.async(execute: {
if let downloadedImage = UIImage(data: data!) {
self.image = downloadedImage
}
})
}).resume()
}
}

然后,您可以在所需的图像上调用它视图:

yourImageView.loadImageUsingUrlString(yourURL)

最新更新