Firebase在swift中上传多个图片

时间:2017-12-07 08:00:36

标签: ios swift firebase firebase-realtime-database firebase-storage

当用户按下按钮时,我想在firebase中上传带有一些文本的多个图像。为此我实现了循环。让我们说如果用户选择了3张照片,那么我认为firebase应该是这样的

 "Post": {
    "uid": {
      "Text": "some text",
      "imageUrl": "url",
      "imageUrl2": "url2",
      "imageUrl3": "url3"
    }
  }

但它会像这样返回。 tweetImageUrl是最后一张照片的第三张照片。也在内部存储中,只有一张照片也是第三张照片。 image here

如何正确上传图片? 这是我的代码。

    func sendTweets() {

         let text = textView.text
         let tweetLength = textView.text.count
         let imagesNumber = imagesArray.count
         format.dateFormat = "yyyy-MM-dd HH:mm"
         let strDate = format.string(from: date as Date)
         let childRef = ref.child("TWEETS").child(uid!).childByAutoId()
         let key = ref.child("TWEETS").child(uid!).childByAutoId().key
         let childStorageRef = storageRef.child("tweetImage").child(uid!).child(key)

  if(tweetLength>0 && imagesNumber>0)
    {

       for image in imagesArray {

        let tweetImage = image
        if let uploadData = UIImagePNGRepresentation(tweetImage) {

         childStorageRef.putData(uploadData, metadata: nil, completion: { (metadata, error) in
                    if error != nil {
                        print("error")
        } else {

        if let imageUrl = metadata?.downloadURL()?.absoluteString {
          let values = ["Text": text!, "tweetImageUrl": imageUrl, "Date": strDate]  as [String : Any]
            childRef.updateChildValues(values)

              }
            }
          })
        }
      }
       dismiss(animated: true, completion: nil)
    }
         else if(tweetLength>0)
         {
            let values = ["Text": text!, "Date": strDate]  as [String : Any]

         childRef.updateChildValues(values)

         dismiss(animated: true, completion: nil)

         }

         else if(imagesNumber>0)
         {

        for image in imagesArray {

            let tweetImage = image
            if let uploadData = UIImagePNGRepresentation(tweetImage) {

             childStorageRef.putData(uploadData, metadata: nil, completion: { (metadata, error) in
                if error != nil {
                    print("error")
                } else {

            if let imageUrl = metadata?.downloadURL()?.absoluteString {
            let values = ["tweetImageUrl": imageUrl, "Date": strDate]  as [String : Any]
            childRef.updateChildValues(values)
                }
             }
          })
        }
      }
        dismiss(animated: true, completion: nil)
  }  else {
        alertTheUser(title: "error", message: "enter something.")
    }
}//func

此外,在上传后检索这些图像并在集合视图上显示时,这是实现这一目标的正确方法吗? 提前谢谢!

2 个答案:

答案 0 :(得分:0)

我认为这不是您在使用firebase时应遵循的正确数据库结构。 如果你存储像imageurl1,imageurl2等那样的图像,那么在不知道密钥的情况下获取这些值真的很困难。 我建议你遵循像

这样的结构
"Post": {
    "uid": {
      "Text": "some text",
      "images": {
         autoID1: url1,
         autoID2: url2
      }
    }
  }

因此,获取这些网址很容易

您可以使用

附加网址
let postRef = Database.database().reference(withPath: "Post").child(uid).child("images")
let autoID = postRef.childByAutoID().key
postRef.child(autoID).setValue(url)

答案 1 :(得分:0)

感谢Rozario的帮助,我明白了。我缺少的是我没有在for循环中声明AutoId。这就是旧价值被新价值取代的原因。下面的代码按照我的预期工作。 Node看起来像这样。希望它会有所帮助。

  "Post": {
    "uid": {
      "Text": "some text",
      "images": {
         autoID1: url1,
         autoID2: url2
      }
    }
  }

以下代码。

func sendTweets() {

             let text = textView.text
             let tweetLength = textView.text.count
             let imagesNumber = imagesArray.count
             format.dateFormat = "yyyy-MM-dd HH:mm"
             let strDate = format.string(from: date as Date)
             let childRef = ref.child("TWEETS").child(uid!).childByAutoId()

      if(tweetLength>0 && imagesNumber>0)
        {

           for image in imagesArray {

            let postRef = childRef.child("tweetImageUrl")
            let autoID = postRef.childByAutoId().key
            let childStorageRef = storageRef.child("Images").child(uid!).child(autoID)
            let tweetImage = image

            if let uploadData = UIImagePNGRepresentation(tweetImage) {

             childStorageRef.putData(uploadData, metadata: nil, completion: { (metadata, error) in
                        if error != nil {
                            print("error")
            } else {

            if let imageUrl = metadata?.downloadURL()?.absoluteString {
                let values = ["Text": text!, "Date": strDate]  as [String : Any]
                let value = [autoID: imageUrl]  as [String : Any]
                childRef.updateChildValues(values)
                postRef.updateChildValues(value)
                  }
                }
              })
            }
          }
           dismiss(animated: true, completion: nil)
        }
             else if(tweetLength>0)
             {
                let values = ["Text": text!, "Date": strDate]  as [String : Any]

             childRef.updateChildValues(values)

             dismiss(animated: true, completion: nil)

             }

             else if(imagesNumber>0)
             {

            for image in imagesArray {

                let postRef = childRef.child("tweetImageUrl")
                let autoID = postRef.childByAutoId().key
                let childStorageRef = storageRef.child("Images").child(uid!).child(autoID)
                let tweetImage = image

                if let uploadData = UIImagePNGRepresentation(tweetImage) {

                 childStorageRef.putData(uploadData, metadata: nil, completion: { (metadata, error) in
                    if error != nil {
                        print("error")
                    } else {

                if let imageUrl = metadata?.downloadURL()?.absoluteString {
                let values = ["Date": strDate]  as [String : Any]
                childRef.updateChildValues(values)
                postRef.child(autoID).setValue(imageUrl)
                    }
                 }
              })
            }
          }
            dismiss(animated: true, completion: nil)
      }  else {
            alertTheUser(title: "error", message: "enter something.")
        }

    }//func