使用 SDWebImage 从 Firebase 存储加载图像文件

Loading an Image file from Firebase Storage using SDWebImage

我正在尝试从我的 Firebase 存储加载图像以显示在我的应用程序视图中。

我主要有2个class

下面是 tableView 数据源扩展。

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.delegate = self
    tableView.dataSource = self
    tableView.register(UINib(nibName: K.tableCellNibName, bundle: nil), forCellReuseIdentifier: K.tableCellIdentifier)
    
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let storageRef = storage.reference()
    let reference = storageRef.child(K.FStore.classImagesPath)
    let placeholderImage = UIImage(named: "placeholder.png")
    let imageView: UIImageView = self.imageView
    let cell = tableView.dequeueReusableCell(withIdentifier: K.tableCellIdentifier, for: indexPath) as! ClassCell
    
        //below is where I try to set the my image but it is not changing anything
        cell.classImage?.sd_setImage(with: reference, placeholderImage: placeholderImage)
      

       //This I tried to make sure that I am can access the image right and it worked just fine
       // cell.classImage?.backgroundColor = .black
    
    return cell
}

图像未使用sd_setImage 方法显示。如果我的代码有任何错误或在任何地方缺少声明,我们将不胜感激。

下面是我 运行 模拟器时得到的结果。这些黑框中显示的不是图像,而是空的。此外,imageView.image 返回 nil,所以很可能 sd_setImage 没有正确放置图像。

self.imgSidebarMenuImage.sd_imageIndicator = SDWebImageActivityIndicator.whiteLarge
self.imgSidebarMenuImage.sd_setImage(with: URL(string: (person["image"] as! String)), placeholderImage: UIImage(named: "logo"))

试试这个

#1

 let storage = Storage.storage().reference(withPath: “YOUR_PATH”)
       storage.downloadURL { (url, error) in
           if error != nil {
               print((error?.localizedDescription)!)
               return
       }
       print(“Download success”)
       
      //url = your will get an image URL
     self.someImageURL = url!
   }

#2

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                // cell identifier methods
 var item = itemData[indexPath.row]

      let referenceImage: StorageReference = storageRef.child("YOUR_PATH")
      let url = URL(string: referenceImage!)!
                    
                    
                    
cell.itemImage?.sd_setImage(with: url, placeholderImage: #imageLiteral(resourceName: "placeholder"))

                }
            }

            return cell
    }