0

example issue - user clicks a button to open all the photos of the chairs iv made. I can hard code all of the image names in, but I assume there is a way for me to update the source folder on fire base with more photos of chairs. and then the next time the user clicks on my chairs gallery button, they will see the new photos I have added, without me having to add the photos directly to the app.

Swift and Firebase

Any ideas?

Here is where I am at the moment, Ideally grabbing a random image from the folder if the switch is turned on. Swift does not like "UIImageView" being a .image, so im not sure whats going on there,

let Switch : Bool = UserDefaults.standard.bool(forKey: "Pictures")
        if Switch == true {
            UIImageView.image = Box1(named: "Sample1\(Int.random(in: 0000...0999))")

            let image0000 = URL(string: "gs://web.appspot.com/Sample1/1000.jpg"),
            let image0001 = URL(string: "gs://web.appspot.com/sample1/1001.jpeg")
            let image0002 = URL(string: "gs://web.appspot.com/Sample1/1002.jpeg")

... and so on

  • If you'e asking how to get a list of files from Firebase Storage, then see: https://stackoverflow.com/questions/37335102/how-to-get-a-list-of-all-files-in-cloud-storage-in-a-firebase-app – Frank van Puffelen Apr 16 '19 at 23:20
  • I see there's an accepted answer but as a more broad answer... Firebase Storage is where you store the images and Firebase Realtime Database (or FireStore) is where you keep a reference to those images. If you don't want to name them, you can leverage .childByAutoId in RTDB to create the key, store the link to the image and the image name would also be that key. I would carefully consider how you are storing the images as you *may* want to name them in the future, or create groups etc and all that can be easily done with the database or FireStore coupled with Storage. – Jay Apr 17 '19 at 17:13
  • The images have names, and they can be named anything specific. I was trying to avoid having to hardcode them back into the app every time they changed. Could you provide sample code for ".childByAutoId". How would I use that method to grab the images in that folder and then declare them images, or display or all images? – Trenton Austin Apr 17 '19 at 22:25
  • The Firebase Getting Started Guide has example code that addresses your question. Conceptually, you would create a new node in the Realtime Database using [.childByAutoId](https://firebase.google.com/docs/database/ios/lists-of-data#reading_and_writing_lists) which will generate a key, that key would then be the name of your image. Then store the image in [Storage](https://firebase.google.com/docs/storage/ios/upload-files), and then keep a reference to the image in the database with that key. Write some code and let us know where you get stuck! – Jay Apr 18 '19 at 14:45
  • I dont know what to write. Their guide seems vague and outdated and im only a month into swift so forgive my ignorance but Iv gotten as far as the button func and the ui for this page. Background firebase stuff is set up and image windows are there. I just have no idea how to, reference the image, download the image, then display it in the view as an image. Like how would I assign the url as an image in my assets so then i may assign it in the view? I would like to grab all the images in the folder and display them but at this point I just want to display an image off anywhere on firebase. – Trenton Austin Apr 18 '19 at 22:09
  • How is the guide outdated? Looks fresh to me... Perhaps you are overlooking the code in the documentation that downloads a file from storage? See [Download Files](https://firebase.google.com/docs/storage/ios/download-files#download_files) – Jay Apr 19 '19 at 17:32
  • Someone said dont use NSURL, use URl and really im like staring at it but theres three things in every box and im not sure if and when to use them all. How would I just make that whole call in one function? No checks or anything, just grab "whatever.jpeg" from fire base and show it in a ui view – Trenton Austin Apr 21 '19 at 07:16
  • Two things; when you want to get someone's attention here on SO, use the @ symbol, like @TrentonAustin, otherwise we wont know you responded. Secondly, not sure what you are referring to NSUrl for. The example code on the Firebase site uses URL as shown here `let localURL = URL(string: "path/to/image")!`. I think you may be overcomplicating it, and should read through the guide I linked in my prior comment. The example specifically shows how to download to memory, or a local file. The path(s) would be stored in RTDB or FireStore, so get each path and download the file. So what's the issue? – Jay Apr 21 '19 at 12:43
  • Oh, and watch the Firecast on [Getting Started With Storage](https://firebase.google.com/docs/storage/ios/start). I think that will clear up a lot of the confusion. – Jay Apr 21 '19 at 12:45
  • thanks @Jay , so basically here is what i have. Based on if the switch is on, it should fetch the image and then display it, the whole display aspect though im having trouble with.$$ let Switch : Bool = UserDefaults.standard.bool(forKey: "Image") if Switch == true { let babies1000 = URL(string: "gs://web.appspot.com/image/1000.jpg"), let image1000 = UIImage $$ – Trenton Austin Apr 24 '19 at 06:05
  • The original idea was to just grab all the images from the folder but instead im just gonna pre name them but subject and then a number, so they can be swapped out without changing the program. The issue was two things, loading all the images without naming them. Maybe a sort of fetch and repeat till the end kinda function. and 2, displaying the images. which is where im stuck rn, the last line of my previous commented code was me trying to declare it as an image – Trenton Austin Apr 24 '19 at 06:10
  • UIImageView.image = Box1(named: "ImageFolder\(Int.random(in: 1000...1999))" ok so this half works, ".image" says uiimageview cant be .image but idk what else it could be. "ImageFolder" would be the file name and then it will randomly select the suffix number and then grab a random url basically. but this is only half the function im guessing, im still stuck on how to actually declare the image view box "box1", as the url defined – Trenton Austin Apr 24 '19 at 06:41
  • My above comment is a simple solution; create a node in the RTDB or Firestore and use that key to name the image which will then be store in Storage. Please don't post code in comments as it's super hard to read. Update your question with the additional info and also take your comments and add them as well as it make the question more clear. – Jay Apr 24 '19 at 15:48
  • Ok, I am not looking to use the real time data base at this point. I have them in storage. I tried and it said my imageview cant be type image. All I know is I have pictures, and I want them in boxes. I will update the top with the function. @Jay – Trenton Austin Apr 25 '19 at 00:02
  • There's really no reason to not leverage Firestore or the RTDB to store those references. You could store them locally as well; creating a unique name is pretty easy with `var image_name = NSUUID().uuidString' and then store that locally in a plist or a preference file. However, leveraging online storage will enable different instances of your app to access the same data - suppose a user is on their phone and wants to switch to their iPad; keeping a reference in Firebase would allow that to seamlessly happen. – Jay Apr 25 '19 at 15:14
  • Oh, and .image is not a file type, [UIImageView](https://developer.apple.com/documentation/uikit/uiimageview) lets you efficiently draw any image that can be specified using a [UIImage](https://developer.apple.com/documentation/uikit/uiimage) object so it could be a .jpg, .png etc. The image refers to the image it contains. In your question we don't know what *Box1* is so that maybe the issue. – Jay Apr 25 '19 at 15:22

0 Answers0