将图像上传到firebase存储

时间:2017-04-27 22:27:22

标签: swift firebase-storage

我正在尝试将两个图片上传到firebase存储,但它只是上传了一个

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")
                }

只有imageOne正在上传。如何一次上传两张图片?

2 个答案:

答案 0 :(得分:2)

试试这样:

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")
               }

在您的代码中,您要将2张图片上传到参考号

希望有所帮助

答案 1 :(得分:1)

我会发表评论,但我还没有足够的声誉。为什么不按照名称建议功能上传一个图像,然后再次调用该函数,第一次传入第一个图像,第二次传入第二个图像。