Search code examples
swiftfirebase-storagesdwebimage

Loading an Image file from Firebase Storage using SDWebImage


I am trying to load an image from my Firebase Storage to be displayed on my app view.

I have 2 classes mainly

  • Main "ClassView" that includes a tableView.
  • A custom tableViewCell - "ClassCell" (This class includes an imageView where I want to display the picture from firestore. - imageView is called classImage.

Below is the tableView Data Source Extension.

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
}

The Image is not displayed using the sd_setImage method. Any help would be highly appreciated if I have any error in my code or missing a declaration anywhere.

Below is what I am getting when I run the simulator. Instead of the image displayed in these black boxes, they are empty. Also, the imageView.image is returning nil so most probably the sd_setImage is not placing the image right.

Class View


Solution

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

    try this

    #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
        }