我无法在Firebase存储桶中下载所有图像文件

时间:2019-07-09 15:29:55

标签: ios swift firebase firebase-realtime-database firebase-storage

我将包含图像的文件夹保存到了Firebase存储中。但我无法获得所有图像URL的列表。我可以一对一地获得它们,但这不是我想要我的应用程序运行的方式。我需要获取所有图像列表URL。我不知道如何将下载URL写入实时数据库并从URL创建存储引用。

 import UIKit
 import Firebase
 import FirebaseAuth
 import FirebaseDatabase
 import FirebaseStorage
 import FirebaseUI


 class PreachersViewController: UIViewController, UIScrollViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate {


    @IBOutlet weak var preacherBannerScrollView: UIScrollView!
    @IBOutlet weak var preacherBannerPageControl: UIPageControl!
    @IBOutlet weak var preacherCollectionView: UICollectionView!
    @IBOutlet weak var preacherTableView: UITableView!



   // Firebase services
   var database: Database!
   var storage: Storage!

  // Initialize an array for your pictures
   var picArray = [UIImage]()


   override func viewDidLoad() {

   loadImageData()


}




    //MARK: Load Preachers Images from FireBase Storage

     func loadImageData() {

        // Initialize Database, Auth, Storage
        database = Database.database()
        storage = Storage.storage()

        let dbRef = database.reference().child("Preachers")
        dbRef.observe(.childAdded, with: { (snapshot) in
            // Get download URL from snapshot
            let downloadURL = snapshot.value as! String
            print(downloadURL)

            // Create a storage reference from the URL
            let storageRef = self.storage.reference(forURL:  downloadURL)   //.referenceFromURL(downloadURL)
            // Download the data, assuming a max size of 1MB (you can change this as necessary)
            storageRef.getData(maxSize: 1 * 1024 * 1024) { (data, error) -> Void in
                // Create a UIImage, add it to the array
                let pic = UIImage(data: data!)
                self.picArray.append(pic!)

                print(pic!)


            }
        })

}

我希望从快照中获取一个URL列表以填充我的数组

0 个答案:

没有答案