如何从Firebase数据库设置配置文件图像 [英] How to set up profile image from firebase database

查看:57
本文介绍了如何从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天全站免登陆