1

I started working with Firebase in my iOS App. I am trying to upload images to Firebase Storage. Fortunately, I was able to upload images to Firebase Storage using Swift 3. My issue is I always need to specify the name of the image in coding.

For e.g.:

let storage = FIRStorage.storage().reference(forURL:
gs://my_app_name.appspot.com/").child("images/new.jpg")

Here, new.jpg is the image name. If I try to upload images multiple times, it gets replaced by the new image but the name remains the same. As a result I always get 1 item in my image folder.

All the example I have seen is for uploading for a single image. In my case the user can upload multiple images.

Is there any method or way so that every time I upload an image to Firebase it gets uploaded with a different image name. Or can I get the name of the image from the iPhone simulator?

Thanks in advance!

AL.
  • 36,815
  • 10
  • 142
  • 281
Gaurav
  • 125
  • 1
  • 9

1 Answers1

1

I found an answer that I would like to share. Hope it may help others.

  1. UUID
let imageUniqueName = UUID().uuidString

let storage = FIRStorage.storage().reference(forURL:
gs://my_app_name.appspot.com/").child("images/\\(imageUniqueName)")

Note: \\(imageUniqueName) can be called with string interpolation

  1. Or

    a) Create a textfield in which the user can give the name of the image he is going to upload while uploading.

    b) store the value of the textfield in a variable.

    c) enter the variable name using above method (String interpolation)

For example:

@IBOutlet weak var imageName: UITextField!

let Name = (imageName?.text)!

let storage = FIRStorage.storage().reference(forURL:
gs://my_app_name.appspot.com/").child("images/\\(Name)")
Moffee
  • 401
  • 5
  • 15
Gaurav
  • 125
  • 1
  • 9