How to set up profile image from firebase database(如何从 Firebase 数据库设置个人资料图片)
问题描述
嘿,我是 Xcode 的新手,需要帮助将我的个人资料图片从 firebase 数据库上传到我的用户个人资料.到目前为止我没有收到任何错误,但是当我的用户登录时图像没有出现,图像已经存储在 firebase 但是当我将 UIImageView 附加到用户配置文件时,图像显示为空白有没有人有解决此代码的解决方案让我的图像在用户登录后出现在用户个人资料中应该是自动的吗?
Hey I am new to Xcode and need of assistance in uploading my profile image from firebase database to my user profile. I have receive no errors so far but the image is not appearing when my user logs in, the image is already stored in firebase but when I attach the UIImageView to the user profile the image shows up blank do anyone have a solution to fix this code to make my image appear on the user profile once they log in it should be automatic?
typealias blockCompletedWith = (Bool, String) -> Void
//path: folder name if any followed by name of image
func uploadImageToFirebaseStorage(data: Data, path: String, blockCompletedWith: @escaping blockCompletedWith) {
let metadata = StorageMetadata()
metadata.contentType = "profileImage.jpg"
let store = Storage.storage()
let storeRef = store.reference().child(path)
let _ = storeRef.putData(data, metadata: metadata) { (metadata, error) in
guard let _ = metadata else {
print("error occured: (error.debugDescription)")
blockCompletedWith(false, "")
return
}
storeRef.downloadURL(completion: { (url, error) in
if let urlText = url?.absoluteString {
blockCompletedWith(true, urlText)
}
else {
blockCompletedWith(false, "")
}
})
}
}
if let profileImageUrl = dictionary?["gs://tunnel-vision-d6825.appspot.com"] as? String {
let url = URL(string: profileImageUrl)
URLSession.shared.dataTask(with: url!) { (data, response, error) in
if let error = error{
print("Error : (error)")
return
}
DispatchQueue.main.async {
self.ProfileImage.image = UIImage(data: data!)
}
}.resume()
}
推荐答案
使用firebase存储来保存图片,并将图片链接放入你的数据库.
Use firebase storage to save images and put the image link into your database.
typealias blockCompletedWith = (Bool, String) -> Void
func uploadProfileImage(_ image: UIImage) {
let imageData = image.jpegData(compressionQuality: 1.0)
let path = "folderName/imagename.jpeg"
self.uploadImageToFirebaseStorage(data: imageData!, path: path, blockCompletedWith: { (isSuccess, urlStr) in
Utility.stopActivityIndicator()
DispatchQueue.main.async {
if isSuccess {
print(urlStr)
}
else {
print("Error in uploading Image")
}
}
})
}
-----------uploadImageToFirebaseStorage 方法------------
-----------uploadImageToFirebaseStorage Method------------
//path: folder name if any followed by name of image
func uploadImageToFirebaseStorage(data: Data, path: String, blockCompletedWith: @escaping blockCompletedWith) {
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"
let store = Storage.storage()
let storeRef = store.reference().child(path)
let _ = storeRef.putData(data, metadata: metadata) { (metadata, error) in
guard let _ = metadata else {
print("error occured: (error.debugDescription)")
blockCompletedWith(false, "")
return
}
storeRef.downloadURL(completion: { (url, error) in
if let urlText = url?.absoluteString {
blockCompletedWith(true, urlText)
}
else {
blockCompletedWith(false, "")
}
})
}
}
这篇关于如何从 Firebase 数据库设置个人资料图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从 Firebase 数据库设置个人资料图片
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01