微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Firebase Firestore iOS:如何将图像保存到Firestore中的集合?

如何解决Firebase Firestore iOS:如何将图像保存到Firestore中的集合?

因此,我有一个应用程序,用户可以在其中向地图添加注释,并且我希望这些创建的注释对所有用户可见并可以访问。我正在尝试将注释数据写入Firestore,但似乎不支持图像。我还尝试将图像转换为base64EncodedString,但随后文档超出了1mb的大小限制。 (通常为10 mb左右)。这是我的代码

let strBase64UserPickedImage =  image?.pngData()?.base64EncodedString()
let strBase64Defaultimage = UIImage(named: "Image-1")?.pngData()?.base64EncodedString()

// User gave both 'warnings' and 'image' values
    if warnings != nil && image != nil {
        db.collection("jumpSpotAnnotations").addDocument(data: [

            "title": title,"coordinate": coords,"estimatedHeight": estimatedHeight,"locationDescription": locationDescription,"warnings": warnings!,"image": strBase64UserPickedImage!

        ]) { (error) in
            if let e = error {
                print("There was an issue saving data to firestore: \(e)")
            } else {
                print("Successfully saved data.")
            }
        }
    }

该“ if”语句有更多部分,但它们无关紧要。如何保存图像而不超出尺寸限制?如果使用Firestore保存图像实际上是不可能的,那么使用Firebase还有另一种方法吗?记住,我正在尝试使每个用户都可以访问这些批注,而不管它是由谁创建的,也不是在尝试设置userDefault或其他任何内容。感谢您的帮助!

解决方法

通常,Firestore不适合图像等大型二进制数据。首选方法是使用Cloud Storage for Firebase存储二进制数据,并使用Firestore将该图像的路径存储在您上传图像的存储桶中。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。