如何从 Firebase 数据库设置个人资料图片 [英] How to set up profile image from firebase database

查看:9
本文介绍了如何从 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 数据库设置个人资料图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆