1
我收到了一個奇怪的問題,我無法從Firebase數據庫檢索個人資料照片。我沒有收到任何錯誤,&出現圖片的ImageView,但其中沒有圖像。我哪裏錯了?正在從Firebase問題中檢索個人資料照片 - Swift
這是我在我的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
---所以我已經解決了這個問題,感謝下面的答案!這裏是下面的代碼,將來任何人都有這個問題!
/** 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))
})
非常感謝!有沒有一種方法可以爲其提供默認圖像?或者我只是加載一個圖像到視圖中,直到下載完成? – AndrewS
沒問題!要提供默認圖像,請將圖像拖放到.xcassets中。在下載之前,請嘗試'self.picture = someImage'並將其添加到您的視圖中。 – Fallah