onevcat / Kingfisher

A lightweight, pure-Swift library for downloading and caching images from the web.
MIT License
23.43k stars 2.66k forks source link

High memory consumption when loading a gif image #2312

Open antonzhigalov opened 1 month ago

antonzhigalov commented 1 month ago

Check List

Thanks for considering to open an issue. Before you submit your issue, please confirm these boxes are checked.

Issue Description

I'm trying to show a gif image in an imageview that results in high memory consumption in the app - over 1GB for a 550KB gif file.

Reproduce

Code example:

import UIKit
import Kingfisher

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        let imageView = UIImageView()
        imageView.frame = self.view.bounds
        imageView.kf.setImage(with: URL(string: "https://blog.elfster.com/wp-content/uploads/2024/09/Wish-of-the-Week-2024-Blog-Header-1.gif")!)

        self.view.addSubview(imageView)
    }
}
onevcat commented 1 day ago

When using UIImageView to display a gif, by default the whole gif frames are loaded into the memory. If you have a gif with huge frames or large in size, it might cause issues.

To get a better memory performance, consider to use the AnimatedImageView.