简体   繁体   English

从 Firebase 加载图像非常慢

[英]Loading image from Firebase is very slow

I am fairly new to Swift development, and am wondering why it is taking so long for the image to show.我对 Swift 开发还很陌生,我想知道为什么要花这么长时间才能显示图像。 It's a 4.44kb image whose URL is stored in a Firestore document.这是一个 4.44kb 的图像,其 URL 存储在 Firestore 文档中。 It takes a second before it shows, which isn't very good for the UX.它需要一秒钟才能显示出来,这对 UX 来说不是很好。 As I said, the image (a barcode) is just 4.44kb, which makes me wonder why loading for example an image from Instagram or Facebook is faster than loading this image.正如我所说,图像(条形码)只有 4.44kb,这让我想知道为什么从 Instagram 或 Facebook 加载图像比加载这张图像要快。

My code:我的代码:

In my viewDidLoad:在我看来DidLoad:

dbRef.getDocument { (document, err) in
        if let e = err {
            print("Error retrieving document: \(e)")
        } else {
            let data = document?.data()
            
            if let imageURL = data?[K.User.barcodeImage] as? String {
                // let url = URL(fileURLWithPath: imageURL)
                let url = NSURL(string: imageURL)
                self.downloadImage(from: url as! URL)
                let number = data![K.User.barcodeNumber] as! String
                self.cardNumber.text = number
                UserDefaults.standard.set(number, forKey: K.User.barcodeNumber)
                UserDefaults.standard.synchronize()
            }
            
            
        }
    }

The rest: rest:

func getData(from url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> ()) {
    URLSession.shared.dataTask(with: url, completionHandler: completion).resume()
}

func downloadImage(from url: URL) {
    getData(from: url) { data, response, error in
        guard let data = data, error == nil else { return }
        DispatchQueue.main.async() { [weak self] in
            self?.cardCode.image = UIImage(data: data)
        }
    }
}

Does this have to do with DispatchQueue?这与 DispatchQueue 有关系吗? Thank you in advance!先感谢您!

The 1 second latency is not unreasonable but you can try enabling cache headers from firebase storage / GCS bucket to improve performance. 1 秒的延迟并非不合理,但您可以尝试从firebase 存储/ GCS 存储桶启用缓存标头以提高性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM