Swift 检索firebase图像-ImageView
大家好,我正在尝试从firebase数据库获取我的图像,并将其作为用户的配置文件图像呈现到图像视图中。我要做的第一件事是创建一个方法来检索数组中的照片数据Swift 检索firebase图像-ImageView,swift,firebase,Swift,Firebase,大家好,我正在尝试从firebase数据库获取我的图像,并将其作为用户的配置文件图像呈现到图像视图中。我要做的第一件事是创建一个方法来检索数组中的照片数据 class PhotoService { static func retrievePhotos(completion: @escaping ([Photo]) -> Void) { //get a databas refrence let db = Firestore
class PhotoService {
static func retrievePhotos(completion: @escaping ([Photo]) -> Void) {
//get a databas refrence
let db = Firestore.firestore()
//get data from "photos" collection
db.collection("photos").getDocuments { (snapshot, Error) in
//check for errors
if Error != nil {
//error in retrieving photos
return
}
//get all the documents
let documents = snapshot?.documents
//check that documents arent nil
if let documents = documents {
//create an array to hold all of our photo structs
var photoArray = [Photo]()
//loop through documents, get a photos struct for each
for doc in documents {
//create photo struct
let p = Photo(snapshot: doc)
if p != nil {
//store it in an array
photoArray.insert(p!, at: 0)
}
}
//pass back the photo array
completion(photoArray)
}
}
}
然后调用该类并尝试在图像视图中显示图像
@IBOutlet var profilePictureImageView: UIImageView!
var photos = [Photo]()
override func viewDidLoad() {
super.viewDidLoad()
//call the photo service to retrieve the photos
PhotoService.retrievePhotos { (retrievedPhotos) in
//set the photo array to the retrieved photos
self.photos = retrievedPhotos
//make the image view a circle
self.profilePictureImageView.layer.cornerRadius = self.profilePictureImageView.bounds.height / 2
self.profilePictureImageView.clipsToBounds = true
//make the image view display the photo
var photo:Photo?
func displayPhoto(photo:Photo) {
//check for errors
if photo.photourl == nil {
return
}
//download the image
let photourl = URL(string: photo.photourl!)
//check for erorrs
if photourl == nil {
return
}
//use url session to download the image asynchronously
let session = URLSession.shared
let dataTask = session.dataTask(with: photourl!) { (data, response, Error) in
//check for errors
if Error == nil && data != nil {
//let profilePictureImageView = UIImage()
let image = UIImage(data: data!)
//set the image view
DispatchQueue.main.async {
self.profilePictureImageView.image = image
}
}
}
dataTask.resume()
}
}
}
}
我不明白我做错了什么,为什么没有显示图像如果有人能向我解释这一点,并给我一个代码示例,这将是令人惊讶的,解释它就像你在向一个五年级的孩子解释一样。在这些步骤中,可以很容易地从Firebase获取图像并将其分配给ImageView 获取
照片URL的函数
//Function to get photo of loggedin User
func getUrl(Completion:@escaping((String)->())) {
let userID = Auth.auth().currentuser?.uid
let db = Firestore.firestore().collection("photos").document(userID)
db.getDocument { (docSnapshot, error) in
if error != nil {
return
} else {
guard let snapshot = docSnapshot, snapshot.exists else {return}
guard let data = snapshot.data() else {return}
let imageUrl = data["photoUrl"] as! String
completion(imageUrl)
}
}
}
下载图像并将其分配给图像视图
//Call this function in VC where your `ImageView` is
func getImage(Url:String){
DispatchQueue.global().async {
let url = URL(string: Url)
if let data = try? Data(contentsOf: url!) {
DispatchQueue.main.async {
self.profilePictureImageView.image = UIImage(data: data)
}
}
}
}
}
在viewDidLoad
中调用这些函数,如下所示:
getUrl{(url) in
getImage(Url:url)
}
在我看来,您的代码是以下主题的直接副本。
收藏(“照片”)
里面有什么?你能用DB向我们展示这个收藏的图片吗?是的,里面是我的照片ID、我的用户ID和我的照片URL。你在Firebase存储中存储照片吗?(如果不是,你应该是)。另外,请不要发布。您刚才问了这个问题。只需从Firebase auth对象获取用户的photoUrl,这可能会容易一些。再见,非常感谢你的程序员!!!如果我见到你,我会给你一个大大的吻