GIFs are amazing. They make us laugh, cry, and are the medium of choice for cat owners worldwide. 🐱

Unfortunately, GIFs can also be a bit of a headache when it comes to performance. Imagine an iOS app that displays a vertically scrolling list of animated GIFs. Making sure each of those GIFs is properly scaled, animating smoothly and handling memory well can be trickier than you might think.

Today we'll check out a library from Reda Lemeden called Gifu that aims to provide a high performance solution for this.

let imageView = AnimatableImageView(frame: CGRect.zero)
imageView.animateWithImage(named: "cat.gif")

It's that simple! Under the hood, AnimatableImageView uses CADisplayLink. It will keep the current frame, as well as the next few frames in memory, ensuring we're good memory citizens. It ends up looking like this:

Gifu also provides a few functions (startAnimatingGIF, stopAnimatingGIF, and isAnimatingGIF) for controlling the actual animation. Nice.

More info about Gifu can be found at git.io/gifu