juelizabeth
juelizabeth

Reputation: 505

uploading images to firebase storage

I am trying to upload two images to firebase storage but it is just uploading one

let imageOne = images[0] as! UIImage
let imageTwo = images[1] as! UIImage

func uploadImage(image: UIImage){
    let randomName = randomStringWithLength(length: 5)
    let randomNames = randomStringWithLength(length: 9)
    let imageData = UIImageJPEGRepresentation(imageOne, 1.0)
    let imageDatas = UIImageJPEGRepresentation(imageTwo, 1.0)
    let uploadRef = FIRStorage.storage().reference().child("images/\(randomName).jpg")

    uploadRef.put(imageData!, metadata: nil) { metadata,
        error in
        if error == nil {
            print("successfully uploaded Image")
            AppDelegate.instance().dismissActivityIndicator()
            self.imageFileName = "\(randomName as String).jpg"

            uploadRef.put(imageDatas!, metadata: nil) { metadata,
                error in
                if error == nil {
                    self.imageFileNameTwo = "\(randomNames as String).jpg"
                } else{
                    print("Error uploading image")
                }

Only imageOne is uploading. How do I uploading two images at once?

Upvotes: 0

Views: 1365

Answers (2)

Vlad Pulichev
Vlad Pulichev

Reputation: 3272

Try like this:

let imageOne = images[0] as! UIImage
let imageTwo = images[1] as! UIImage

func uploadImage(image: UIImage){
    let randomName = randomStringWithLength(length: 5)
    let randomNames = randomStringWithLength(length: 9)
    let imageData = UIImageJPEGRepresentation(imageOne, 1.0)
    let imageDatas = UIImageJPEGRepresentation(imageTwo, 1.0)
    let uploadRef = FIRStorage.storage().reference().child("images/\(randomName).jpg")

    uploadRef.put(imageData!, metadata: nil) { metadata,
        error in
        if error == nil {
            print("successfully uploaded Image")
            AppDelegate.instance().dismissActivityIndicator()
            self.imageFileName = "\(randomName as String).jpg"

            randomName = randomStringWithLength(length: 5)
            let uploadRef2 = FIRStorage.storage().reference().child("images/\(randomName).jpg")
            uploadRef2.put(imageDatas!, metadata: nil) { metadata,
                error in
                if error == nil {
                    self.imageFileNameTwo = "\(randomNames as String).jpg"
                } else{
                    print("Error uploading image")
               }

In your code you are uploading 2 images to the one ref.

Hope it helps

Upvotes: 3

Zach Fuller
Zach Fuller

Reputation: 1267

I would post as a comment but I don't have enough reputation yet. Why don't you just make the function upload one image as the name suggests, and then call the function twice passing in the first image the first time and then the second image the second time.

Upvotes: 1

Related Questions