firebase存储图像下载非常慢

发布于 2025-02-05 07:05:51 字数 1854 浏览 1 评论 0原文

我正在开发一个iOS应用程序,其中我的帖子从Firestore数据库中获取。每个帖子都包含对存储相应图像的火箱存储的引用。当我想下载图像时,大约10-15秒才需要下载它们。我将它们加载不同步。我尝试通过

  • firebase sdk getdata()方法
  • URL背后的内容
  • 下载它们下载URL下载它们,然后通过将URL传递到AsyncimageView下载

,但是,这些方法都没有获得任何好的结果,这些结果可以用于不错的UX 。我该如何更快地做到这一点?

以前的答案建议将存储公开...不是通过URL显示公开方法吗?

如果不是,我必须公开它,我如何防止每个人都能看到每个图像,无论它是应用程序的用户。是否有可能拥有“公共”存储空间,但仍然不能让每个人都可以使用?

我应该更改为其他提供商吗?

代码:

func orderedImageDownload3(imageRefs: [String], doc: QueryDocumentSnapshot){
    let group = DispatchGroup()
    var images = [UIImage]()
    let storageRef = Storage.storage().reference()
    for ref in imageRefs {
        let fileRef = storageRef.child(ref)
        group.enter()
        fileRef.downloadURL { url, error in
            if let error = error {
                // Handle any errors
                print(error)
            } else {
                //Do the download
                if let url = url {
                    self.getImage(from: url) {data, response, error in
                        guard let data = data, error == nil else { return }
                        print(response?.suggestedFilename ?? url.lastPathComponent)
                        print("Download Finished")
                        // always update the UI from the main thread
                        if let image = UIImage(data: data){
                            images.append(image)
                            group.leave()
                        }
                    }
                }
            }
        }
    }
    group.notify(queue: .main) {
        //put images into observable object
    }
    }
    
    
func getImage(from url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> ()) {
        URLSession.shared.dataTask(with: url, completionHandler: completion).resume()
    }
}

I am developing an IOS app in which I have posts that get fetched from the firestore database. Each post contains references to the firebase storage, where the corresponding images are stored. When I want to download the images, it takes ages for them to be downloaded, around 10-15 seconds. I load them asynchronous. I tried downloading them via

  • the firebase SDK getData() method
  • downloading the url and then downloading the content behind the URL
  • downloading them via passing the url into an asyncImageView

However, none of these methods achieve any good results that could be used for a decent UX. How can I make this faster?

Previous answers suggested making the storage public... Isn't displaying them via the URL a public method?

If it is not and I have to make it public, how can I prevent that everybody can see every image, whether it is a user of the app or not. Is it possible to have a "public" storage but still not making it accessible for everyone?

Should I change to a different provider?

Code:

func orderedImageDownload3(imageRefs: [String], doc: QueryDocumentSnapshot){
    let group = DispatchGroup()
    var images = [UIImage]()
    let storageRef = Storage.storage().reference()
    for ref in imageRefs {
        let fileRef = storageRef.child(ref)
        group.enter()
        fileRef.downloadURL { url, error in
            if let error = error {
                // Handle any errors
                print(error)
            } else {
                //Do the download
                if let url = url {
                    self.getImage(from: url) {data, response, error in
                        guard let data = data, error == nil else { return }
                        print(response?.suggestedFilename ?? url.lastPathComponent)
                        print("Download Finished")
                        // always update the UI from the main thread
                        if let image = UIImage(data: data){
                            images.append(image)
                            group.leave()
                        }
                    }
                }
            }
        }
    }
    group.notify(queue: .main) {
        //put images into observable object
    }
    }
    
    
func getImage(from url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> ()) {
        URLSession.shared.dataTask(with: url, completionHandler: completion).resume()
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文