1

This is how I upload to Firebase. But it takes a while to fetch it back. Also while saving it too, that's why I tried asynchronising the process.

if imagePathToUpload != nil {
   let uploadImgPath = Firebase(url:"\(rootURL)/users/\(id!)")
   let imageData = UIImagePNGRepresentation(image)
   let base64String = imageData!.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
   let imageRef = uploadImgPath.childByAppendingPath("\(path!)")

    dispatch_async(dispatch_get_main_queue(), {
         imageRef.setValue(base64String)
    })
  }                

The image size is 370x370 so it shouldn't take that long in my opinion. or should I try to smaller the image before I upload it?

What is the proper way of handling image storage to Firebase?

senty
  • 12,385
  • 28
  • 130
  • 260
  • I've done this method you are using with few issues before on firebase. I think my images were only 50x50 or something like that. Another method would be to save the url on firebase, If say, they are profile pictures of user accounts. And then, with the help of a third party library like SDWebImage, you can load the pictures from the url's and SDWebImage will handle caching for you so that your not doing a bunch of networking requests if the image is being used in a lot of places. – NSGangster Apr 19 '16 at 13:06
  • as a side note it is unnecessary to fetch the main queue when running a `imageRef.setValue()` the firebase sdk will handle asynchronous connections between itself and your code. – NSGangster Apr 19 '16 at 13:08
  • You probably don't want to use Firebase to store images; use another service that's designed to store them. However, small images, such as a thumbnail is pretty easy. See [Swift2 retrieving images from Firebase](http://stackoverflow.com/questions/33644560/swift2-retrieving-images-from-firebase/34044446#34044446) for a super simple example. – Jay Apr 19 '16 at 17:53

1 Answers1

3

What is the proper way of handling image storage to Firebase?

As covered in many questions before: the Firebase JSON Database is not a good fit for storing images.

The best way to handle user-generated images is to store them on a dedicated files/image storage service and then store the URL of that service in your Firebase Database.

Update: At I/O 2016 Firebase introduced Firebase Storage, which is a perfect fit for storing images.

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • i have used firebase for that purpose before. could you tell me WHY it's not a good fit? works pretty well. and you have any recommendations of what to use instead? – David Seek Dec 05 '16 at 21:45
  • If is perfectly feasible to store and retrieve images in the Firebase Database without problems. I've built some very performant image carousels on it, precisely to prove that it's possible. But storing unstructured data in a JSON tree is essentially still quite wasteful. You're essentially using a quite expensive storage mechanism to store "dumb" bytes. A better fit is Firebase Storage, which we release at I/O. I've updated my answer to reflect that. – Frank van Puffelen Dec 06 '16 at 05:23
  • indeed i AM using firebase storage. it's working perfectly for me and I thought you were talking about that in your answer. that is why i was confused and wanted to ask you. thank you for your time and answer – David Seek Dec 06 '16 at 05:27