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 数据库设置个人资料图片


- Android - 拆分 Drawable 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- android 4中的android RadioButton问题 2022-01-01