neofliesgtt
neofliesgtt

Reputation: 55

Fetching Image Url's From Realtime Database - Firebase / Swift

I am trying to display images onto a collection view. The image url's are saved in the realtime database in Firebase and was wondering, what I am doing wrong? Essentially, users have a node called 'Images' where images they have uploaded are saved inside that node. The names depend on the image Title and the url is the image they have uploaded. Below is my code and a photo of how my database looks like it. If someone could help me out, I would appreciate that a lot. Thanks!

My ViewController:

class PicturesViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
    
    @IBOutlet weak var imageCollection: UICollectionView!
    var customImageFlowLayout: CustomImageFlowLayout!
    var images = [UserImages]()
    
    var dbRef: DatabaseReference!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        dbRef = Database.database().reference().child("users/\(Auth.auth().currentUser!.uid)/Images")
        
        loadDB()      
    }
    
    func loadDB() {
        
        dbRef.observe(DataEventType.value, with: { (snapshot) in
            
            var newImages = [UserImages]()
            
            for UserImagesSnapshot in snapshot.children {
                
                let UserImagesObject = UserImages(snapshot: UserImagesSnapshot as! DataSnapshot)
                newImages.append(UserImagesObject)
                
            }
            
            self.images = newImages
            self.imageCollection.reloadData()
            
        })
        
        
    }
    
    func collectionView(_ imageCollection: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            
        return images.count
        
    }
    
    func collectionView(_ imageCollection: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = imageCollection.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! ImageCollectionViewCell
        
        let image = images[indexPath.row]
                
        cell.imageView.sd_setImage(with: URL(string: image.url), placeholderImage: UIImage(named: "image1"))
        return cell
    } 

My Array - UserImages

struct UserImages {
    
    let key: String!
    let url: String!
    
    let itemsRef: DatabaseReference!
    
    init (url:String, key:String) {
        
        self.key = key
        self.url = url
        self.itemsRef = nil
        
    }
    
    init(snapshot: DataSnapshot) {
        
        key = snapshot.key
        itemsRef = snapshot.ref
        
        let snapshotValue = snapshot.value as? NSDictionary
        
        if let imageUrl = snapshotValue?[""] as? String {
            
            url = imageUrl
                       
        } else {
            
            url = ""
            
        }
    }    
}

This is how the realtime database looks like

Upvotes: 0

Views: 161

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

Replace

    let snapshotValue = snapshot.value as? NSDictionary
    
    if let imageUrl = snapshotValue?[""] as? String {
        
        url = imageUrl
                   
    } else {
        
        url = ""
        
    }

with

 url = snapshot.value as! String 

Upvotes: 1

Related Questions