从Firebase问题中检索配置文件图片 - Swift

时间:2017-06-14 16:35:28

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

我遇到了一个奇怪的问题,我无法从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上的样子 enter image description here

---所以我已经通过以下答案解决了这个问题!以下是代码,以防任何人在将来遇到此问题!

/** 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))


    })

1 个答案:

答案 0 :(得分:1)

对于Firebase,您无法使用firebase存储的URL。如果您决定将图像存储到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()
  }
 }