【问题标题】:How do I upload images to Firebase Storage and get link for downloadURL?如何将图像上传到 Firebase 存储并获取下载 URL 的链接?
【发布时间】:2017-12-03 21:07:09
【问题描述】:

如何将两张图片上传到 Firebase 存储并一次获取下载 URL 的链接...由于某种原因,我的代码只能将一张图片上传到数据库。这是我的代码以及 imagePicker 代码。请记住,第二张图片是可选的。

@IBAction func pickImage1(_ sender: Any) {


    let image = UIImagePickerController()
    image.delegate = self
    image.sourceType = UIImagePickerControllerSourceType.photoLibrary
    image.allowsEditing = false
    selected = 1

    self.present(image, animated: true)
}

//Add didFinishPickingMediaWithInfo here
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        if selected == 1 {
            myImageView1.image = image
        } else {
            myImageView2.image = image
        }
    }
    else {
        //error
    }
    self.dismiss(animated: true, completion: nil)
}


@IBAction func pickImage2(_ sender: Any) {

       let image2 = UIImagePickerController()
    image2.delegate = self
    image2.sourceType = UIImagePickerControllerSourceType.photoLibrary
    image2.allowsEditing = false
    selected = 2

    self.present(image2, animated: true)
}




@IBAction func upload(_ sender: Any) {


    if let image1 = myImageView1.image {
        guard let data = UIImagePNGRepresentation(image1) else { return }

        let storageRef = Storage.storage().reference().child("images/\(NSUUID().uuidString)/image.png")
        storageRef.putData(data, metadata: nil, completion: { (metadata, error) in

            if error != nil {
                print("error")
                return

            }


            else {
                let downloadURL = metadata?.downloadURL()?.absoluteString


                self.ref?.child("Posts").childByAutoId().setValue(["Title": self.titleText.text, "Subtitle": self.subtitleText.text, "Article": self.articleText.text, "Author": self.authorText.text, "Date": self.dateText.text, "Tags": self.tagsText.text, "PostType": self.postType.text, "PostStyle": self.postStyle.text, "PostSize": self.postSize.text, "Download URL": (downloadURL)])

             return
            }



        })
    }

    if let image2 = myImageView2.image {
        guard let data = UIImagePNGRepresentation(image2) else { return }

        let storageRef = Storage.storage().reference().child("images/\(NSUUID().uuidString)/image1.png")
        storageRef.putData(data, metadata: nil, completion: { (metadata, error) in
            if error != nil {
                print("error")
                return

            }


            else {
                let downloadURL = metadata?.downloadURL()?.absoluteString
                let downloadURL2 = metadata?.downloadURL()?.absoluteString

                self.ref?.child("Posts").childByAutoId().setValue(["Title": self.titleText.text, "Subtitle": self.subtitleText.text, "Article": self.articleText.text, "Author": self.authorText.text, "Date": self.dateText.text, "Tags": self.tagsText.text, "PostType": self.postType.text, "PostStyle": self.postStyle.text, "PostSize": self.postSize.text, "Download URL": (downloadURL), "Download URL 2": (downloadURL2)])


            }

        })
    }

}

【问题讨论】:

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


    【解决方案1】:

    试试

    首先使用

    获取要下载的图像的引用 URL
    let starsRef = storageRef.child("images/pic.jpg")
    
    // Fetch the download URL
    starsRef.downloadURL { url, error in
      if let error = error {
        // Handle any errors
      } else {
        // Get the download URL for 'images/stars.jpg'
      }
    }
    

    参考 Generate a download URL SWIFT

    【讨论】:

      【解决方案2】:

      尝试为存储引用使用不同的名称。例如第二个:

      if let image2 = myImageView2.image {
          guard let data = UIImagePNGRepresentation(image2) else { return }
      
          let storageRef2 = Storage.storage().reference().child("images/\(NSUUID().uuidString)/image1.png")
          storageRef2.putData(data, metadata: nil, completion: { (metadata, error) in
              if error != nil {
                  print("error")
                  return
              } else {
                  let downloadURL = metadata?.downloadURL()?.absoluteString
      
                  self.ref?.child("Posts").childByAutoId().setValue(["Title": self.titleText.text, "Subtitle": self.subtitleText.text, "Article": self.articleText.text, "Author": self.authorText.text, "Date": self.dateText.text, "Tags": self.tagsText.text, "PostType": self.postType.text, "PostStyle": self.postStyle.text, "PostSize": self.postSize.text, "Download URL": (downloadURL), "Download URL 2": (downloadURL2)])
              }
          })
      }
      

      希望对你有帮助

      【讨论】:

      • 还是只上传第一个downloadURL
      猜你喜欢
      • 1970-01-01
      • 2018-01-24
      • 2021-12-27
      • 2019-06-08
      • 2021-10-27
      • 2021-03-23
      • 1970-01-01
      • 2019-03-24
      • 2018-03-23
      相关资源
      最近更新 更多