How can i use library Nuke in Swift UIKit (storyboards) to load images from firestore database

44 Views Asked by At

i've tryed some methods but they are not right, so i have some code, this is my main file, where i fetch data from firebase and also use object mapper to map data from firestore, here i use func which is from another file, this is configure func:

import UIKit
import FirebaseFirestore
import ObjectMapper

class BeetlesCollectionController: UICollectionViewController {
    var arrayOfBeetles: Array<Beetle> = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.delegate = self
        fetchDataFromFirestore()
        navigationItem.title = "Beetles"
        let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.black]
        navigationController?.navigationBar.titleTextAttributes = textAttributes
      
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.sectionInset = UIEdgeInsets(top: 10,left: 10,bottom: 5,right: 10)
    }
    
    
    @IBOutlet var myCollectionView: UICollectionView!
    
    func fetchDataFromFirestore() {
        let db = Firestore.firestore()

        db.collection("beetles").getDocuments { (snapshot, error) in
            if let error = error {
                print("Error fetching data: \(error.localizedDescription)")
            } else {
                if let documents = snapshot?.documents {
                    for document in documents {
                        let data = document.data()
                        print(document.data())
    
                        if let beetle = Mapper<Beetle>().map(JSON: data) {
                            self.arrayOfBeetles.append(beetle)
                        }
                    }
                    self.myCollectionView.reloadData()
                } else {
                    print("No documents found.")
                }
            }
        }
    }
    
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return arrayOfBeetles.count
    }
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let beetleCell = collectionView.dequeueReusableCell(withReuseIdentifier: "BeetleColletionCell", for: indexPath) as? BeetleColletionCell else {
            return UICollectionViewCell()
        }

        beetleCell.configure(with: arrayOfBeetles[indexPath.row])
        return beetleCell
    }


    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let vc = storyboard?.instantiateViewController(identifier: "NavigatedBeetlesInfo") as? NavigatedBeetlesInfo {
            let beetleSelected = arrayOfBeetles[indexPath.row]
            vc.name = beetleSelected.name ?? "none"
            navigationController?.pushViewController(vc, animated: true)
        }
    }

   
}

And this is my custom cell where i'm trying to show data:

import UIKit
import Nuke
import NukeExtensions



class BeetleColletionCell: UICollectionViewCell {
    
    @IBOutlet weak var imageBeetleView: UIImageView!
    @IBOutlet weak var beetleName: UILabel!
    
    override func awakeFromNib() {
          super.awakeFromNib()
          layer.cornerRadius = 20
          layer.masksToBounds = true
      }

    func configure(with beetle: Beetle?) {
        beetleName.text = beetle?.name ?? "none"
    }
    /*
    
    func imageShow(for url: URL) async throws -> PlatformImage{
        let image = try await ImagePipeline.shared.image(for: url)
        return image
    }
     */
    
}
0

There are 0 best solutions below