什么是正确的Firebase设计,用于在多个位置显示相同的图像

问题描述 投票:0回答:1

我有一个表视图,当单击一个单元格(类别)时,它会显示一个带有2个选项卡的标签栏。第一个选项卡是相机,第二个选项卡是带有一些图像的图像库。

主要约束是当在特定单元(类别)中更新图像时,它可能属于另一个类别,并且也应该在那里更新。

现在,我将实时数据库和存储设置为模仿类别的目录。更新图像(在特定单元格内)时,它会更新存储中的图像(通过利用单元格的属性),并将数据库中的指针指向直接下载链接。

这种方法的问题在于它根本不处理主要约束,并且最终存储了同一图像的多个副本。

这是我将图像上传到Firebase的代码

func savePhotoStorage(){
    //replace spaces with _
    let title = textField.text!.replacingOccurrences(of: " ", with: "_")
    let imageRef = storageRef.child(self.section).child(self.AU).child("Photos").child(title + ".jpg")

    if let dataImage = UIImageJPEGRepresentation(imageView.image!, 1){

        let uploadTask = imageRef.putData(dataImage, metadata: nil, completion : { (metadata, error) in
            if error != nil{
                print(error)
                return
            }
            if let downloadPath = metadata?.downloadURL()?.absoluteString{
                self.pushtoDB(path: downloadPath,type:"Photos",title: title)
                print(metadata?.downloadURL())
            }
            else{
                print("No metadata available")
            }
        })
        uploadTask.resume()
        textField.text = ""
        //captureSession.stopRunning()
        //previewLayer.removeFromSuperlayer()
    }
}

func pushtoDB(path : String, type:String,title:String){
    databaseRef.child(section).child(AU).child(type).child(title).setValue(path)
}

这是我上传图片时更新图库的代码

func loadImages(){
    databaseHandle = databaseRef.observe(DataEventType.childAdded, with: { (snapshot) in
        if let _ = snapshot.value as? String {

            let imageView = UIImageView()
            let url = URL(string: snapshot.value as! String)
            let resource = ImageResource(downloadURL: url!, cacheKey: snapshot.value as? String)
            imageView.kf.indicatorType = .activity
            imageView.kf.setImage(with: resource)
            imageView.contentMode = .scaleAspectFit

            let label = UILabel()
            //label.text = imageArray[i]
            let title = snapshot.key.replacingOccurrences(of: "_", with: " ")
            label.text = title
            label.font = UIFont(name: "ScienceFair", size: 30)
            label.textAlignment = .center
            label.textColor = UIColor.white
            label.backgroundColor = UIColor.black
            label.sizeToFit()

            let width = UIScreen.main.nativeBounds.width/2
            let xPosition = width * CGFloat(self.i)
            imageView.frame = CGRect(x: xPosition, y: 0, width: self.mainScrollView.frame.width, height: self.mainScrollView.frame.height)
            label.frame = CGRect(x: xPosition, y: 10, width: label.intrinsicContentSize.width, height: label.intrinsicContentSize.height)
            label.center = CGPoint(x: xPosition+self.view.center.x, y: 30)

            self.mainScrollView.contentSize.width = self.mainScrollView.frame.width * CGFloat(self.i+1)
            self.mainScrollView.addSubview(imageView)
            self.mainScrollView.addSubview(label)
            self.mainScrollView.autoresizingMask = .flexibleHeight

            self.i = self.i+1
        }

    })
}

对我来说完美的用例是,如果有一些方法可以将监听器连接到Firebase存储,如果文件位置更新到新的下载链接,RealTime数据库中指向旧下载链接的所有子项现在指向到新的下载链接。

到目前为止,我还没有找到这种类型的监听器,所以我应该如何配置我的存储和数据库,以便上述情况可以发生,我不必上传相同图像的副本?

ios swift firebase
1个回答
2
投票

您只需要在存储中拥有一个映像副本,并且只需在数据库中为它创建多个引用,所有这些都使用相同的下载URL。然后,您可以使用database.ref('images').orderByChild(databaseURL).equalTo(imageDatabaseURL)等查询在数据库中更新它们

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.