Search code examples
swiftfirebasefirebase-storagefirebaseuisdwebimage

FirebaseStorageUI + sdwebimage


I've updated firebase pods today and I've encountered some issues. First of all I was getting this error:

No such module 'FirebaseStorageUI'

Now I've changed it into:

import FirebaseStorage
import FirebaseUI

This seems to work as all functions are still available like storage and sd_webimage.

Now I'm getting this error:

Cannot convert value of type 'StorageReference' to expected argument type 'URL?'

Below code worked before the update

let storageRef = Storage.storage().reference().child("user/thomas_1")
self.profileImageView.sd_setImage(with: storageRef, placeholderImage: UIImage(named: "placeholder"), completed: { (image, error, cacheType, storageRef) in
    if image != nil && error != nil {
        UIView.animate(withDuration: 0.3) {
            self.profileImageView.alpha         = 1
        }
    }
})

Now it didn't and it gave me that error, I changed it into a url but it still doesn't work or is giving me errors. Following the examples in the documentation and sample apps above method should work. I also found out that the url is a direct gs:// path and that is not reachable.

If anyone can point out (maybe the obvious) mistake that would be very helpful and thanks in advance.


Solution

  • You will need to get the download URL of the image, which can be obtained through the downloadURL method, like this:

    let storageRef = Storage.storage().reference().child("user/thomas_1")
    
    storageRef.downloadURL { url, error in
      guard let url = url else { return }
      self.profileImageView.sd_setImage(with: storageRef, placeholderImage: UIImage(named: "placeholder"), completed: { (image, error, cacheType, storageRef) in
        if image != nil && error != nil {
          UIView.animate(withDuration: 0.3) {
            self.profileImageView.alpha = 1
          }
        }
      })
    }
    

    This will work, but instead of generating the download URL every time like the above example, I'd recommend generating it right after initially uploading the image, and then storing this download URL in whatever database you use.