0

I need to randomize images that are located in Firebase Storage. My initial thought was to use the download links that are provided by Firebase for each image but not sure if that is the best solution. For test purposes I dropped three images into my Xcode project and I've been randomizing those locally but now I need to pick from images in Firebase. I've included the code from the view controller below. I didn't see anything that might answer this question so any thoughts would be greatly appreciated.

import UIKit
import FBSDKCoreKit
import FBSDKLoginKit
import FirebaseDatabase

class SecondViewController: UIViewController {

var ref:FIRDatabaseReference!

@IBOutlet weak var sliderLabel: UILabel!


@IBOutlet weak var verticalSlider: UISlider!

       {
            didSet{
                verticalSlider.transform = CGAffineTransform(rotationAngle: CGFloat(-M_PI_2))

            }
    }

@IBAction func verticalSliderChanged(_ sender: UISlider) {

        var currentValue = Int(sender.value);

        sliderLabel?.text = "\(currentValue)"

}


@IBAction func checkmarkButtonPressed(_ sender: Any) {


    let img:UIImageView = UIImageView(frame: CGRect(x: 30, y: 180, width: 250, height: 320))

    let randomImage = arc4random()

    if (randomImage % 4 == 0) {
        img.image = UIImage(named: "hero.jpg")
    } else if (randomImage % 3 == 1) {
        img.image = UIImage(named: "hillary.jpg")
    } else {
        img.image = UIImage(named: "thistimeisdifferent.png")

    //self.ref.child(uuid).child("Book Info").child("Book Title").child("Slider Value").setValue(currentValue)

    }

}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

0

There is no way to get a list of the file in Cloud Storage through the Firebase SDK. So you'll have to keep that list elsewhere.

Right now you seem to do that in your code/project, but to scale that you could keep a list of the files' download URLs in something like the Firebase Database. Then you'd select a random child from that list.

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807