【问题标题】:Swift and Firebase - Receiving image from the user then uploading it to firebaseSwift 和 Firebase - 从用户那里接收图像,然后将其上传到 firebase
【发布时间】:2022-02-10 16:14:32
【问题描述】:

我希望用户从照片库上传一张图片以及更多输入,以便我可以将其写入我的数据库。 我用从用户那里接收到的数据创建了一个对象,然后将其上传(使用“.child()”)到 firebase 实时数据库,但后来我不得不将照片添加到对象中,所以我使用 UIImagePicker 上传照片然后我被困。 我的问题是我不知道应该从图像中获取什么类型的信息,因此我可以将其添加到对象中。 我不确定使用对象是否是正确的选择,但由于我添加的数据与特定项目相关,我认为它是合适的。

// 对象

 let object: [String : Any] = ["areaname": areaName! as Any ,"spotNo": spotNo, "loactionLat": areaLat, "locationLong": areaLong]
    database.child("Areas").child("Area_\(Int.random(in: 0..<100))" ).setValue(object)

@IBAction func chooseImageButton() {
    print("Add image button was pressed")
    let vc = UIImagePickerController()
    vc.sourceType = .photoLibrary
    vc.delegate = self
    vc.allowsEditing = true
    present(vc, animated: true)
 
}


extension AddAreaViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediawithInfo info: [UIImagePickerController.InfoKey : Any]) {
    print("\(info)")
    if let image = info[UIImagePickerController.InfoKey(rawValue: "UIImagePickerControllerEditedImage")] as? UIImage {
    }
    
    picker.dismiss(animated: true, completion: nil) }

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    picker.dismiss(animated: true, completion: nil)
}

}

Firebase structure

【问题讨论】:

  • 如果您尝试存储图像,您应该使用 Firebase 存储,然后在数据库中保存对它的引用。

标签: swift xcode firebase firebase-realtime-database uiimagepickercontroller


【解决方案1】:

我的解决方案是使用 FirebaseStorage 保存图像。并将图像 url 转换为字符串写入数据库。当您需要加载图像时。您可以通过该 urlstring 进行搜索。

【讨论】:

  • 如何将 url 作为字符串获取?并将其发送到填充数据库对象的另一个函数?
  • 获取 URL 和类型转换很简单。let url = info[UIImagePickerController.InfoKey.imageURL] as? URL``let imageURL: String = url.path。如果你想把它发送到另一个函数。您可以查看有关 Swift 函数值和返回的文档。
【解决方案2】:

我认为@Evan Lu 是正确的。 您不需要将图像存储在 firebase 数据库或实时数据库中。 您可以将图像存储在 FirebaseStorage 中并将 url 保存在数据库中。

【讨论】:

  • 这可以添加为评论而不是答案。
  • 抱歉,我还不能对其他问题发表评论。因为我的个人资料不足以发表评论。
猜你喜欢
  • 1970-01-01
  • 2018-04-09
  • 2021-03-19
  • 2017-07-28
  • 2016-10-13
  • 1970-01-01
  • 1970-01-01
  • 2019-06-02
  • 2019-06-24
相关资源
最近更新 更多