ios - 如何覆盖 firebase 存储 url 值 Swift 4

标签 ios swift firebase firebase-storage

嘿伙计们,在检查了堆栈溢出的所有内容并查看了您可以在此处找到的文档之后:

https://firebase.google.com/docs/storage/ios/delete-files

我仍然不知道如何更改 url 上的值我有这段代码:

  let uploadData = UIImageJPEGRepresentation(self.profileImage.image!, 0.3)

            staticValuesForData.instance.dataBaseUserref.child(self.currentUserUid).observeSingleEvent(of: .value , with: { (snapshot) in
                if let imageName = snapshot.childSnapshot(forPath: staticValuesForData.instance.profilePicStorageUrl).value as? String {
                    let storageRef = Storage.storage().reference().child("profile_images").child("\(imageName)")



                    storageRef.putData(uploadData!, metadata: nil, completion: { (metaData, error) in
                        if error != nil{
                            print(error!)
                            return
                        }

                        if let profileImageURL = metaData?.downloadURL()?.absoluteString{

                            staticValuesForData.instance.dataBaseUserref.child(self.currentUserUid).child(staticValuesForData.instance.profileUrl).setValue("\(profileImageURL)")

                        }

                    })

                }
            })

什么都不做,然后更改我在 firebase 存储中的个人资料图片子图像,如下所示:

enter image description here

名称保持不变以覆盖之前的图像(profilePicStorageUrl 是每个用户唯一的字符串)

我的问题是,为什么当我用新图片替换现有图片时“链接”(https://firebase..。)无法加载新图片,但它具有相同的名称并且似乎在 Firebase 存储中被覆盖?

enter image description here

**详细问题:**

据我所知,上面的配置文件 url 和 picUrl 都指向存储中的同一点,但是当旧图片被新图片覆盖时,我的 firebase 数据库中仍然具有旧 url 的对象不会加载新图片,为什么?是否有解决此问题的方法或我可以使用旧网址并覆盖其中的值的任何方式?谢谢你的帮助

更新

问题可以归结为这个,我觉得这样会更容易让路过的人更快地理解。

storageRef.putData(uploadData!, metadata: nil, completion: { (metaData, error) in
                        if error != nil{
                            print(error!)
                            return
                        }

                        if let profileImageURL = metaData?.downloadURL()?.absoluteString{

                            staticValuesForData.instance.dataBaseUserref.child(self.currentUserUid).child(staticValuesForData.instance.profileUrl).setValue("\(profileImageURL)")

                        }

                    })

此元数据有一个 downloadstring() 方法,但我似乎找不到更改下载字符串的方法,如何将下载字符串设置为创建用户时创建的现有下载字符串?这是解决上述问题的方法吗?

最佳答案

我已经在评论中回答了,但我在这里添加它(更容易看到)以防其他人有同样的问题或需要一些关于这个问题的指南。

The thing is that url is changed each time new file is uploaded: that is why I never saved image URL in database but instead saved image storage KEY. The thing is that Firebase provides library to load images by just giving file reference. It is very simple and I have implemented it few times already, works nice!

Check it out here: https://firebase.google.com/docs/storage/ios/download-files#downloading_images_with_firebaseui

关于ios - 如何覆盖 firebase 存储 url 值 Swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48406276/

相关文章:

ios - 如何在 faSTLane 中处理多个 iOS 目标(小部件)

ios - 单元格中的图像显示在 textLabel 和 detailTextLabel 上方,但在附件 View 下方

ios - 授予地址簿访问权限后加载 View

javascript - 是否可以将 firebase 的图片上传 "uploadTask.on"监听器变成一个 promise?

android - 如何使Firebase用户在Google登录功能之外 flutter 朔迷离?

objective-c - 自定义 UITableView 不刷新单元格

ios - 单击 UITableView 中的标签时,tableview 单元格中的 IndexPath 返回错误

ios - 如何使用 Swift 在 AVCaptureVideoPreviewLayer 的特定区域捕获二维码数据?

ios - Swift 3 - 如何将 Real Result<Object> 转换为泛型类型 [T]

javascript - 设置 firebase 后出现以下错误 : Client doesn't have permission to access the desired data