大家好,我正在尝试从Firebase数据库中获取我的图片,并将其作为用户的个人资料图片呈现到图片视图中。我要做的第一件事是创建一个方法来检索数组中的照片数据
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()
}
}
}
}
我不明白我做错了什么以及为什么图像没有显示,如果有人可以向我解释这一点并给我一个令人惊叹的代码示例,解释它就好像你在向 5 年级的孩子解释它
一样在以下步骤中,可以轻松完成从Firebase获取图像并将其分配给ImageView的快速方法。
获取PhotoUrl
的功能
//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)
}
}
}
To download image and assign it to image view
//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)
}