5

I have a single method that should deal with downloading images. It seems to be working completely correctly, outside of the fact that the Data returned from Firebase Storage is 0 bytes. Why is this?

 func useDatabaseToDownloadPicture () {
     if let userId = FIRAuth.auth()?.currentUser?.uid {

    // Get a reference to the storage service using the default Firebase App
    let storage = FIRStorage.storage()

        let firebaseImages = FIRStorage.storage().reference().child("Images")
        let userPhotoLocation = firebaseImages.child("Users").child(userId).child("profilePicture.jpg")
        let myRef = FIRStorage.storage().reference()
        let firebaseProfilePicLocation = firebase.child("Users").child(userId).child("Images").child("profilePicture").child("downloadURL")

    firebaseProfilePicLocation.observe(.value, with: { (snapshot) in
        print(" Profile Picture Databse Snapshot -> \n \(snapshot) ")
        // Get download URL from snapshot
        if let downloadURL = snapshot.value as? String {
             print(" downloadURL converted to String ")

        // Create a storage reference from the URL
        let storageRef = storage.reference(forURL: downloadURL)

        // Download the data, assuming a max size of 1MB (you can change this as necessary)
        storageRef.data(withMaxSize: 1 * 1024 * 1024) { (data, error) -> Void in
            print("Downloading Data . . . Data String -> \(String(describing: data))")
            print("Downloading Data . . . Data Literal -> \(data)")
            // Create a UIImage, add it to profile picture
            if data != nil {
            print("Data is not nil!")
                if let image = UIImage(data: data!) {
                    print("Image Created")
                } else {
                    print("Image Could Not create!! check data...")
                    let url = URL(string: downloadURL)
                    let urlData = try? Data(contentsOf: url!)
                    if let img = UIImage(data: urlData!) {
                        print("Image  2 Created")
                    } else {
                        print("Image 2 Failed")
                    }
                }
            let pic = UIImage(data: data!)
            self.profilePicture.image = pic
            } else {
                print(" ❌ Data is nil ")
            }
        }



        } else if let downloadURL = snapshot.value as? URL {
              print(" downloadURL converted to URL! ")

                // Create a storage reference from the URL
                let storageRef = storage.reference(forURL: downloadURL.absoluteString)

                // Download the data, assuming a max size of 1MB (you can change this as necessary)
                storageRef.data(withMaxSize: 1 * 1024 * 1024) { (data, error) -> Void in
                    // Create a UIImage, add it to profile picture
                    if data != nil {
                        let pic = UIImage(data: data!)
                        self.profilePicture.image = pic
                    } else {
                        print(" ❌ Data is nil")
                    }
                }
        } else {

             // . . . Do nothing, all ways failed
        }

    })
    }
}

The result printed is:

downloadURL converted to String
Downloading Data . . . Data String -> Optional(0 bytes)
Downloading Data . . . Data Literal -> Optional(0 bytes)
Image Could Not create!! check data...
Image 2 Failed

To me, it seems that the problem is less the data coming in properly, but it seems as it the problem is the data being 0 bytes. Why is that the case?

Thank you!!

KENdi
  • 7,576
  • 2
  • 16
  • 31
Ryan Cocuzzo
  • 3,109
  • 7
  • 35
  • 64
  • Why do you have 2 firebase reference for 1 profile picture? The path is also different since 1 starts with Users and one with Images. You just could download the image and set it for the profilePicture with ~4 lines. – J. Doe Jul 11 '17 at 14:35
  • To me that just seemed visually appropriate, it was more of a preference. @J.Doe – Ryan Cocuzzo Jul 11 '17 at 16:33
  • The link (downloadURL), can you verify that really is an image? – J. Doe Jul 17 '17 at 13:44
  • The error checking is kind of in the wrong place. The very first line in the closure should be something like *if let error = error {//handleError} else {//no errors}* that may help catch errors in general. Also, the downloadURL is suspect so adding *print(downloadURL)* right after the if statement will tell us if in fact, it's a valid URL. Try those couple of things and see what the result is. – Jay Jul 18 '17 at 19:34
  • @RyanCocuzzo Can you update your answer and paste that URL? I bet something goes wrong there and the path isn't correct. – ZassX Jul 21 '17 at 12:37
  • @ZassX when I open the url, it saves the picture to my desktop so the URL is working – Ryan Cocuzzo Jul 30 '17 at 23:06

0 Answers0