0
votes

I have trouble retrieving a UIImage from Firebase Storage, the child path seems to be correct, though the image does not get "downloaded" to be displayed. The part about the Firebase Database is working fine, hence retrieving data, whereas the Storage one is now. Code and Firebase path below

I cannot understand whether the problem is in the fact that I nested the function into the .observeSingleEvent of the Database retrieving function or not.

gs://xxxyyy-xxxyyy.appspot.com/images/QhRmIcbF7AOWjZ3nrjFd7TOekrA3/FirstImage.jpg

var cells : [Cella] = []

 var imageReference: StorageReference {
        return Storage.storage().reference().child("images")
    }


 var databaseReference: DatabaseReference {
     return Database.database().reference()
 }

func getDataFromFirebase() -> [Cella]{

        let queryRef = databaseReference.queryLimited(toLast: 1)
        var appCells : [Cella] = []

        queryRef.observeSingleEvent(of: .value, with: { (snapshot) in

            for snap in snapshot.children {
                var userPhoto : UIImage?
                let userSnap = snap as! DataSnapshot
                let customerUid = userSnap.key
                let userDict = userSnap.value as! [String:AnyObject]
                let description = userDict["description"] as! String
                let title = userDict["title"] as! String

                print(title)
                print(String(customerUid))
                print(description)

                self.descriptionsArray[String(customerUid)] = description
                self.titlesArray[String(customerUid)] = title
                 //error is here BECAUSE it can't retrive the image to be dispalyed. Title and description are fine
                self.imageReference.child(String(customerUid)).child("FirstImage.jpg").getData(maxSize: 10*1024*1024, completion: { (data, error) in

                    if error != nil {
                        print("\(String(describing: error?.localizedDescription))")
                    }

                    else {userPhoto = UIImage(data: data!)}


                    })
                let newCella = Cella(image: userPhoto!, title: title, bodyMessage: description)
                appCells.append(newCella)

                }
            })

        return appCells

    }

------ UPDATE ------ As suggested I changed to using firebase Firestore and saving there the download URL as well as the other information. Still though, I cannot seem to get the image downloading. New code below.

This is the data retrieved by document.data() : [email protected] => ["userID": QhRmIcbF7AOWjZ3nrjFd7TOekrA3, "userDescription": Route66, "userImageUrl": https://firebasestorage.googleapis.com/v0/b/shardana-61183.appspot.com/o/images%2FQhRmIcbF7AOWjZ3nrjFd7TOekrA3%2FFirstImage.jpg?alt=media&token=dea541bf-d598-414e-b4ed-a917541598d5, "userTitle": Sample]

firestoreUsersDatabase.getDocuments { (querySnapshot, error) in
        if let error = error {
            print("Error getting the documents: \(error)")
        } else {
            for document in querySnapshot!.documents {
                print("\(document.documentID) => \(document.data())")
                let data = document.data()
                let imageUrl = data["userImageUrl"] as! String
                let title = data["userTitle"] as! String
                let description = data["userDescription"] as! String

                let urlDownloadReference = self.imageReference.reference(forURL: imageUrl)

                urlDownloadReference.getData(maxSize: 10*2014*2014, completion: { (data, error) in

                    if error != nil {
                        print("An error occurred: \(String(describing: error?.localizedDescription))")
                    } else {

                        guard let imageDownloaded = UIImage(data: data!) else {print("Image url returned nil value ERROR"); return}

                        let newCell = Cella(image: imageDownloaded, title: title , bodyMessage: description )
                        print("NEW CELL: Image \(newCell.image)")
                        appCells.append(newCell)

                    }
                })

            }
        }
    }
1

1 Answers

0
votes

yes, I think you're logic needs review. You need to store on your Firestore all the users data, including all the references to needed images. On the other hand, Firebase Storage, which is a different service within Firebase will save the images an will give you download links, but it uses a different logic than Firestore.

See the following example for clarification on what I mean:

https://firebase.google.com/docs/storage/web/download-files