【发布时间】: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 存储,然后在数据库中保存对它的引用。
标签: swift xcode firebase firebase-realtime-database uiimagepickercontroller