I am developing a photo gallery app in Swift right now using Firebase to store the images. My goal is to load all the images in a collection view when the view loads.
我正在使用Firebase在Swift中开发一个照片库应用程序来存储图像。我的目标是在视图加载时加载集合视图中的所有图像。
The images are stored in folders called images_(user email)_(user id). The user uploads images which are stored in the Firebase Storage with the names image0.png, image1.png, image2.png, etc. Right now, I have successfully created accounts and stored the images. Now, I am trying to load the images in a collection view in the viewDidLoad(). I am not using the Firebase Realtime Database because I feel it would be overkill for what I am trying to accomplish.
图像存储在名为images_(用户电子邮件)_(用户ID)的文件夹中。用户上传存储在Firebase存储中的图像,其名称为image0.png,image1.png,image2.png等。现在,我已成功创建帐户并存储图像。现在,我正在尝试在viewDidLoad()的集合视图中加载图像。我没有使用Firebase实时数据库,因为我觉得这对我想要完成的事情来说太过分了。
while (isDownloading == true) {
let storage = Storage.storage()
let storageRef = storage.reference();
let imageLoadRef = storageRef.child(("images_" + (Auth.auth().currentUser?.email)! + "_" + (Auth.auth().currentUser?.uid)!) + "/image" + String(self.imageCount) + ".png")
imageLoadRef.getData(maxSize: 10 * 1024 * 1024) { data, error in
if let error = error {
self.isDownloading = false
} else {
let loadedImage = UIImage(data: data!)
//Updates collection view with loaded image
self.collectionView.reloadData()
//moves on to next image
self.imageCount += 1
}
}
}
As soon as the view loads, I run a while loop that iterates through all the images in the user's folder. Since I don't have any way to get the number of images in the folder, I had to use a while loop and break it when the next image is not found. For example, if there are only 5 images in the folder, the code will return an error when it tries to load the sixth nonexistent image. When this happens, I try to catch the error and break the while loop by setting the condition isDownloading to false.
一旦视图加载,我运行一个while循环,遍历用户文件夹中的所有图像。由于我无法获取文件夹中的图像数量,因此我不得不使用while循环并在找不到下一个图像时将其中断。例如,如果文件夹中只有5个图像,则代码在尝试加载第6个不存在的图像时将返回错误。发生这种情况时,我尝试捕获错误并通过将条件isDownloading设置为false来中断while循环。
However, the issue I am running in to is that the while loop never breaks even though it should return an error and set isDownloading to false.
但是,我遇到的问题是while循环永远不会中断,即使它应该返回错误并将isDownloading设置为false。
To clarify, the imageCount variable is increased with each iteration of the while loop to get the next image in the folder. So when imageCount = 1 for example, the code will try to load a photo with name "image1.png". However, if it doesn't exist, it catches the error and breaks the while loop.
为了澄清,imageCount变量随着while循环的每次迭代而增加,以获得文件夹中的下一个图像。因此,当imageCount = 1时,代码将尝试加载名为“image1.png”的照片。但是,如果它不存在,它会捕获错误并中断while循环。
I know the way I structured it is a little confusing but I find it more efficient than using downloadURLs and the Firebase Database.
我知道我构建它的方式有点令人困惑,但我发现它比使用downloadURL和Firebase数据库更有效。
Any help is appreciated. Thank you!
任何帮助表示赞赏。谢谢!
1 个解决方案
#1
0
Do not know will it work or not you can just give it a try For now and let me know if issue exists
不知道它是否有效你可以尝试一下现在,如果问题存在,请告诉我
//Loop is Fine
while (isDownloading == true) {
//Provided Required References
let storage = Storage.storage()
let storageRef = storage.reference();
//Name of file to download
let imageLoadRef = storageRef.child(("images_" + (Auth.auth().currentUser?.email)! + "_" + (Auth.auth().currentUser?.uid)!) + "/image" + String(self.imageCount) + ".png")
//handler That Run in async Mode
imageLoadRef.getData(maxSize: 10 * 1024 * 1024) { data, error in
//Here I think mistake is
if let error = error {
//You are catching error But not handling data error
self.isDownloading = false
}
else {
if data != nil{ // if image found
//here you need to check weather this handler is returning data o not as you got error of no data So Do All functioning here
// User photo here
let loadedImage = UIImage(data: data!)
//Updates collection view with loaded image
self.collectionView.reloadData()
//moves on to next image
self.imageCount += 1
}
else {
//If Image is not found This will cause you an error which you will handle , According to me this was need to be handled
self.isDownloading = false
}
}
}
}
In case this solution is not working You can check Below Demo project on GitHub and use this Method to easily manage images stored under a user
如果此解决方案不起作用您可以在GitHub上检查Below Demo项目并使用此方法轻松管理存储在用户下的图像
Link - https://github.com/RockinGarg/FirebaseDemo
#1
0
Do not know will it work or not you can just give it a try For now and let me know if issue exists
不知道它是否有效你可以尝试一下现在,如果问题存在,请告诉我
//Loop is Fine
while (isDownloading == true) {
//Provided Required References
let storage = Storage.storage()
let storageRef = storage.reference();
//Name of file to download
let imageLoadRef = storageRef.child(("images_" + (Auth.auth().currentUser?.email)! + "_" + (Auth.auth().currentUser?.uid)!) + "/image" + String(self.imageCount) + ".png")
//handler That Run in async Mode
imageLoadRef.getData(maxSize: 10 * 1024 * 1024) { data, error in
//Here I think mistake is
if let error = error {
//You are catching error But not handling data error
self.isDownloading = false
}
else {
if data != nil{ // if image found
//here you need to check weather this handler is returning data o not as you got error of no data So Do All functioning here
// User photo here
let loadedImage = UIImage(data: data!)
//Updates collection view with loaded image
self.collectionView.reloadData()
//moves on to next image
self.imageCount += 1
}
else {
//If Image is not found This will cause you an error which you will handle , According to me this was need to be handled
self.isDownloading = false
}
}
}
}
In case this solution is not working You can check Below Demo project on GitHub and use this Method to easily manage images stored under a user
如果此解决方案不起作用您可以在GitHub上检查Below Demo项目并使用此方法轻松管理存储在用户下的图像
Link - https://github.com/RockinGarg/FirebaseDemo