简体   繁体   中英

How can I download an image from Firebase to Swift and then display it in a UIImageView more quickly?

I am downloading images from Firebase to Swift (most are around 5 MB) and then displaying them in a UIImageView. Each one only appears once, so there is no need to cache them for later viewing. Most of the time, they take a second or two to load. Are there ways of making this time shorter?

My code:

Database.database().reference().child("movies").child(movieNumString).observeSingleEvent(of: .value, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
    let UrlString: String? = ((dictionary["posterPhotoURL"] as? String))
    let Url = URL(string: (UrlString)!)

    self.moviePosterImageView.sd_setImage(with: Url, placeholderImage: #imageLiteral(resourceName: "PImage"), options: [], completed: { (image, error, cacheType, url) in
            self.moviePosterImageView.image = image
    })
}

Please see stack overflow guidelines for future.

Most importantly, you should worry about the data and size of the images, the time that it takes to download them will decrease as you decrease the size. Try to decrease the sizes down to some kb.

In Firebase you can have functions that runs server side and for instance, you can perform resizing on the images. See here for all the function examples and for resizing this is the example using sharp package.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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