如何从Firebase数据库设置个人资料图像

问题描述 投票:0回答:1

[嘿,我是Xcode的新手,需要帮助将我的个人资料图像从Firebase数据库上传到我的用户个人资料。到目前为止,我还没有收到任何错误,但是当我的用户登录时该图像没有出现,该图像已经存储在Firebase中,但是当我将UIImageView附加到用户个人资料时,该图像显示为空白,是否有人可以解决此代码?使我的图像在用户登录后自动出现在用户个人资料中?

               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()

                        }
ios image firebase firebase-realtime-database uiimageview
1个回答
1
投票

使用Firebase存储来保存图像并将图像链接放入数据库。

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方法------------]

//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, "")
                }
            })
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.