1
votes

I want to upload two copies of each image, one fullsize and one thumbnail. I have looked for answers here on SO and the Firebase docs.

With the code below, only the thumbnail image is saved to storage. How can I fix it so that both images are uploaded? Obviously I haven't fully understood how to work with returns/closures. Help much appreciated!

This series of methods gets called when user taps the upload button

self.uploadAndGetFullSizeImageDownloadURL(completion: { (url) in

                print("Fullsize URL: \(url) is stored")
                self.fullSizeDownloadURL = url

                self.uploadAndGetThumbNailImageDownloadURL(completion: { (url) in
                    print("Thumbnail URL: \(url) is stored")
                    self.thumbNailDownloadURL = url

                    // Save image info to database
                    self.saveImageInfoToDatabase(fullSizeURL: self.fullSizeDownloadURL, thumbNailURL: self.thumbNailDownloadURL, completion: { (finished) in

                        ...
                    })
                })
            })

The methods for uploading and getting the URLs look like this:

func uploadAndGetFullSizeImageDownloadURL(completion: @escaping (String) -> Void) {

    var fullUrlStr = ""

    // Get imageData
    let fullSizeImageData = self.getImageData(size: "fullSize")

    // Upload fullsize image to storage
    let fullSizeStorageRef = self.sRef.child("\(String(describing: uid!)), \(createdAt!)")
    fullSizeStorageRef.putData(fullSizeImageData, metadata: nil, completion: {
        (metadata, error) in

        print("Fullsize image is stored")

        // Get the fullsize downloadURL
        fullSizeStorageRef.downloadURL(completion: { (url, error) in
            if let downloadURL = url?.absoluteString {
                fullUrlStr = downloadURL
                //Return fullsize downloadURL
                completion(fullUrlStr)
            }
        })
    })
}
1
Can you show what you get printed in the console? I mean the messages for: print("Fullsize URL: \(url) is stored") and print("Thumbnail URL: \(url) is stored") – vauxhall
The url prints the same string as the downloadURL you can view in the storage console. Obviously I have only been able to compare it for the thumbnail image (since no fullsize image gets uppladed) but I must assume it is correct. – Dan Abnormal
So, a URL to the fullsize image is retrieved, but no image is saved in storage. I'm thinking that the putData-method isn't finishing for the fullsize, but I can't understand why. – Dan Abnormal

1 Answers

1
votes

Found a way: How to upload multiple image on firebase using swift?

It’s a different approach but works perfectly.