【问题标题】:Loading an Image file from Firebase Storage using SDWebImage使用 SDWebImage 从 Firebase 存储加载图像文件
【发布时间】:2023-03-22 04:29:01
【问题描述】:

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

我主要有两门课

  • 包含 tableView 的主“ClassView”。
  • 自定义 tableViewCell - “ClassCell”(此类包括一个 imageView,我想在其中显示来自 firestore 的图片。- imageView 称为 classImage。

下面是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 没有正确放置图像。

【问题讨论】:

  • 这段代码实际上在做什么,而不是你所期望的?请编辑问题以使其更清晰,并包含您的调试详细信息。
  • 确保您尝试访问的图像是 ios 支持的格式,而不是 SVG。
  • @Sreekuttan 它是 JPG 格式。所以我猜它是支持的
  • @DougStevenson 该代码未显示任何图像。只显示一个白框。当我尝试访问 imageView.image 时。它给零。将在问题中添加详细信息。

标签: swift firebase-storage sdwebimage


【解决方案1】:
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
    }

【讨论】:

  • 我添加了上面的行,但我仍然得到空的 imageViews
  • @Zeyad 您的图像字符串正确吗?还是您的图片格式正确?
  • sd_setImage(with:reference) 中的引用字符串与 Firestore 上的存储位置完全相同。它可能与访问令牌有关吗?
  • @Zeyad 我编辑了我的答案。尝试其中任何一种。希望它有效。
  • 你是如何将 StorageRef 类型的 referenceImage 作为字符串传递到下一行的?
猜你喜欢
  • 1970-01-01
  • 2017-06-23
  • 1970-01-01
  • 1970-01-01
  • 2020-06-28
  • 2019-11-25
  • 1970-01-01
  • 2021-12-28
  • 2020-11-06
相关资源
最近更新 更多