【问题标题】:Retrieving Profile Picture from Firebase issue - Swift从 Firebase 问题中检索个人资料图片 - Swift
【发布时间】:2017-11-16 22:16:26
【问题描述】:

我遇到了一个奇怪的问题,我无法从 firebase 数据库中检索个人资料图片。我没有收到任何错误,并且出现了图片的 ImageView,但是其中没有图像。我哪里错了?

这是我的 ViewWillAppear 中的代码:

/** Profile Picture **/
        profilePicture.frame = CGRect(x: self.view.frame.width / 2.975, y: self.view.frame.height / 3.925, width: self.view.frame.width / 3 , height: self.view.frame.height / 5)
        profilePicture.layer.borderColor = UIColor.black.cgColor
        profilePicture.layer.borderWidth = 3
       // profilePicture.backgroundColor = UIColor.purple
        profilePicture.layer.cornerRadius = profilePicture.layer.frame.width/2




   let ref = FIRDatabase.database().reference().child("users").child(FIRAuth.auth()!.currentUser!.uid)

    ref.child("pictureforprofile").observe(.value, with: {(snap: FIRDataSnapshot) in


        let imageUrl = snap.value as! String
        print(imageUrl)


        self.profilePicture.sd_setImage(with: URL(fileURLWithPath: imageUrl))

        self.view.addSubview(self.profilePicture)
        self.reloadInputViews()




    })

当我打印出 imageUrl 时,它显示为:

 https://firebasestorage.googleapis.com/v0/b/wavelength-official.appspot.com/o/profilePicture%2F4348A9F6-A49B-4FF4-BC14-83081684E8FA.jpg?alt=media&token=a842bda6-59b0-42dc-bf24-8dbb839e7231

这就是我的数据库在 Firebase 上的样子

--- 所以我已经解决了这个问题,感谢下面的回答!这是下面的代码,以防将来有人遇到这个问题!

/** Profile Picture **/
    profilePicture.frame = CGRect(x: self.view.frame.width / 2.975, y: self.view.frame.height / 3.925, width: self.view.frame.width / 3 , height: self.view.frame.height / 5)
    profilePicture.layer.borderColor = UIColor.black.cgColor
    profilePicture.layer.borderWidth = 3
    // profilePicture.backgroundColor = UIColor.purple
    profilePicture.layer.cornerRadius = profilePicture.layer.frame.width/2

    let ref = FIRDatabase.database().reference().child("users").child(FIRAuth.auth()!.currentUser!.uid)

    ref.child("pictureforprofile").observe(.value, with: {(snap: FIRDataSnapshot) in

        let imageUrl = snap.value

        let storage = FIRStorage.storage()
        _ = storage.reference()
        let ref = storage.reference(forURL: imageUrl as! String)
        ref.data(withMaxSize: 1 * 1024 * 1024) { data, error in
            if error != nil {
                // Uh-oh, an error occurred!
            } else {

                self.profilePicture.image = UIImage(data: data!)
                self.view.addSubview(self.profilePicture)
                self.reloadInputViews()
            }
        }


       // self.profilePicture.sd_setImage(with: URL(fileURLWithPath: imageUrl as! String))


    })

【问题讨论】:

    标签: firebase swift3 firebase-realtime-database uiimageview firebase-storage


    【解决方案1】:

    对于 Firebase,您不能将 URL 用于您的 Firebase 存储。如果您决定将图像存储到 IMGUR 或 FTP 服务器上,那么您想出的解决方案可以正常工作。但是,您必须使用从 firebase 检索到的 url 并使用他们的 Storage API 下载它

    // Download in memory with a maximum allowed size of 1MB (1 * 1024 * 1024 bytes)
    let storage = Storage.storage()
    let storageRef = storage.reference()
    let ref = storage.reference(forURL: imageURL)
    ref.getData(maxSize: 1 * 1024 * 1024) { data, error in
       if let error = error {
         // Uh-oh, an error occurred!
      } else {
    
         self.profilePicture = UIImage(data: data!)
        self.view.addSubview(self.profilePicture)
        self.reloadInputViews()
      }
     }
    

    【讨论】:

    • 非常感谢!有没有一种方法可以为它提供默认图像?还是我只是将图像加载到视图中,直到下载完成?
    • 没问题!要提供默认图像,请将图像拖放到您的 .xcassets。在下载之前尝试“self.picture=someImage”并将其添加到您的视图中。
    猜你喜欢
    • 2021-01-15
    • 2018-09-05
    • 1970-01-01
    • 2018-12-20
    • 2012-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多