0
votes

Please help me. Explain how to set the image in the cell. In my Database I have: title, description and imageURL (url from Firebase Storage). Can you write me a code and explain.

class TrainingProgram
{
    var description = ""
    var title = ""
    var imageURL = ""


    init(description: String, title: String, imageURL: String) {
        self.description = description
        self.title = title
        self.imageURL = imageURL

    }

function to get data from firebase.

func fetchPrograms() {
        Database.database().reference().child("programs").observe(.childAdded) { (snapshot) in
            if let dict = snapshot.value as? [String: AnyObject] {
                let newTitle = dict["title"] as! String
                let newDescription = dict["description"] as! String
                let newImageURL = dict["imageURL"] as! String
                let trainingCell = TrainingProgram(description: newDescription, title: newTitle, imageURL: newImageURL)
                self.trainingPrograms.append(trainingCell)
                DispatchQueue.main.async {
                    self.collectionView.reloadData()

                }
            }
        }

    }

And this is how I set title and description to my cells.

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TrainingProgramCollectionViewCell", for: indexPath) as!  TrainingProgramCollectionViewCell

        //cell.featuredImageView.setImage(from: indexPath.item)
        cell.titleLabel.text = trainingPrograms[indexPath.item].title
        cell.descriptionLabel.text = trainingPrograms[indexPath.item].description

What I need to write in the function for receiving data to get images and how to set images in cells

structureOfDatabase

1
You want to show images on your tableView Cell? - Wings
@wings Yes. I can't understand how to show images from firebase in my cells - Stormios
@wings Can you write me a code, which will fetch url from FirebaseDatabase( DB has url from Storage) and show on cells ?) - Stormios
@wings I've added structure of DB - Stormios

1 Answers

1
votes

Install SDWebImage pod from this link https://github.com/SDWebImage

And in Your CollectioView cellForRow method

    var images_list = [String]()
    var imagesArray = [URL]()
    images_list.append(itemModel[indexPath.row]. imageURL)
    let storage = Storage.storage().reference()

    for x in images_list{
        print(x)

        let storageRef = storage.child("images/\(x).jpg")
        storageRef.downloadURL { (url, error) in
            if let error = error{
                print(error.localizedDescription)
            }
            else{
                imagesArray.append(url!)
            }
            if let x = images_list.last{
              cell.itemImage.sd_setImage(with: URL(string: x), placeholderImage: UIImage(named: "default"))
            }
        }
    }

Full code

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
   {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TrainingProgramCollectionViewCell", for: indexPath) as!  TrainingProgramCollectionViewCell

    //cell.featuredImageView.setImage(from: indexPath.item)
    cell.titleLabel.text = trainingPrograms[indexPath.item].title
    cell.descriptionLabel.text = trainingPrograms[indexPath.item].description

    var images_list = [String]()
    var imagesArray = [URL]()
     for x in images_list{
        print(x)

        let storageRef = storage.child("images/\(x).jpg")
        storageRef.downloadURL { (url, error) in
            if let error = error{
                print(error.localizedDescription)
            }
            else{
                imagesArray.append(url!)
            }
            if let x = images_list.last{
              cell.itemImage.sd_setImage(with: URL(string: x), placeholderImage: UIImage(named: "default"))
            }
        }
    }

 }

Try this for single image downloadJust take your imageUrl from firebase

 let x: String = "yourImageUrl" 
 let storageRef = storage.child("images/\(x).jpg")
        storageRef.downloadURL { (url, error) in
            if let error = error{
                print(error.localizedDescription)
            }
            else{
                imagesArray.append(url!)
            }
            if let x = images_list.last{
              cell.itemImage.sd_setImage(with: URL(string: x), placeholderImage: UIImage(named: "default"))
            }
        }