如何上传到Firebase数据库,使其按时间戳排序,而不是按字母顺序排序

时间:2018-03-02 06:12:45

标签: ios swift firebase firebase-realtime-database firebase-storage

当用户在我的应用中拍照时,它会上传到存储,然后下载链接会保存到数据库中。在不同的VC上,所有这些图像都显示在图库中。

问题在于,上传新图片时,Firebase会按字母顺序对子项进行排序。 enter image description here

enter image description here

如何按时间戳上传图像,以便最新图像显示在底部?请记住,snapshot.key值是图像的标题,因此我无法更改这些值。

这是我上传图片的方式

 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 loadImages(){
    newImageHandle = 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
        }

    })
}

0 个答案:

没有答案