Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading GIFs in iOS consumes too much memory

I've been testing around a lot of open-source animated-gif libraries to load GIF files into our Swift project.

Most of them claim to be high-performance libraries, however, whenever I load an animated gif my application uses around 8MB of memory.

The problem is that this dedicated memory space seems to never be released. We can see it growing linearly:

enter image description here

And it makes me wonder if I'm doing the right thing here. Is this behavior correct, or is it potentially bad for the user?

like image 330
Machado Avatar asked Feb 06 '17 18:02

Machado


People also ask

Why do GIFs slow down Iphone?

The main reason why your GIFs load so slowly is likely because you have too many frames in the GIF. Next time, delete one frame for every two that you use.

How many MB can a GIF be?

Photos can be up to 5MB, animated GIFs can be up to 5MB on mobile, and up to 15MB on web. Supported image types are GIF, JPEG, and PNG files.

How do I reduce the framerate of a GIF?

To change the speed of a GIF in Photoshop, first, open your GIF file, then go to Window > Timeline. In the Timeline panel, click the first frame, hold Shift, then click the last frame to select the whole GIF. Now click the frame delay setting and choose a new time to speed up or slow down your GIF.

What is the max duration of a GIF?

How long can an animated GIF be? The same, there is no restriction on the length of a GIF. While some platforms have ruled it. (Such as, GIPHY suggests uploading a GIF no more than 6 seconds and it won't accept a 15 seconds plus GIF.)


1 Answers

you would need to remove the Gif images manually for them to get cleared from the memory.

Example: gifViwer can be your Gif Viewer in this example

self.gifViewer.removeFromSuperview()
self.gifViewer = nil

if you had to assign a delegate then you can also add

self.gifViewer.Delegate = nil

Note: you need to run this when the viewControler is being completely unloaded as the app will start looking for gifViewer in the view while it has been removed. if you need to clear the gif image from the memory without unloading the view then I suggest to use the steps above and then adding the view to the superView programatically. this is not going to work with the interface designer and IBOutlets

let gifViewer = yourGifViewerClass(frame: CGRect(x: 20, y: 20, width: self.view / 2, height: self.view / 2))
    //  setup your gifViewer
like image 142
Mentos Avatar answered Oct 13 '22 21:10

Mentos