Home > other >  How to download user image from firebase to UI
How to download user image from firebase to UI

Time:01-04

Hey I need help on how to restructure this code to view my UIimage. I am trying to download the Profile image from firebase but Im not sure how to set up this code to get my image back. The code im using is not allowing me to view my image I put breakpoints on every line and I received no error What am I missing from this code that will allow me to retrieve my image data and view it as my UIImage on the simulator

The collection is named user and the document is ProfileImages the same name as the folder in firebase storage and the data is name profilePics the same as in firebase storage as well

 let db = Firestore.firestore()
                        let docRef = db.collection("user").document("ProfileImages")
                        
                        docRef.getDocument { (document, error) in
                            if let doc = document,
                               let filename = doc.get("profilePic") as? String {
                                let stg = Storage.storage()
                                let remotePath  = "user/ProfileImages/\(filename)"
                                
                                
                                 stg.reference(withPath: remotePath).getData(maxSize: 1_000_000) { (data, error) in
                                                                  if let remoteData = data,

                                   
                                       self.ProfileImage.image == UIImage(data: remoteData) {
                                        
                                    }

CodePudding user response:

Try this

import UIKit
    import FirebaseStorage
    let storageRef = Storage.storage().reference().child("<your-image-name>")
    storageRef.downloadURL { (url, error) in
      guard let url = url else {
        // Handle the error
        return
      }
      imageView.contentMode = .scaleAspectFit
      imageView.sd_setImage(with: url, completed: nil)
    }

CodePudding user response:

First, don't save the https path of the image in the document as the image's reference. This is a bad idea. Instead, save the name of the image as the value in profilePic in the document. Therefore, upload a new image to Firebase Storage called testImage (don't put it in any folders, just upload it at the root level).

Second, change the value of profilePic in your document to equal testImage.

Third, change your code to the following and let us know what the outcome is including the print statements if it doesn't work.

let db = Firestore.firestore()
let docRef = db.collection("user").document("ProfileImages")

docRef.getDocument { (document, error) in
    if let error = error {
        print("error getting document")
    }
    if let doc = document,
       let filename = doc.get("profilePic") as? String {
        print("document data obtained")
        let stg = Storage.storage()
        let remotePath  = filename
        
        stg.reference(withPath: remotePath).getData(maxSize: 10 * 1048576) { (data, error) in
            if let error = error {
                print("error getting image")
            }
            if let remoteData = data {
                print("image data obtained")
                self.ProfileImage.image == UIImage(data: remoteData)
            } else {
                print("no image data")
            }
        }
    } else {
        print("no document data")
    }
}

This code assumes your image is less than 10 MB in size. If it's bigger, change the code accordingly. But for testing purposes, just pick an image that is smaller than 10 MB.

  • Related