This minimalistic update to DTFoundation adds a helper method for loading small animated GIFs. It’s a hotfix release because no other functionality was modified.
- ADDED: DTAnimatedGIF in separate static lib / subspec
- FIXED: DTActionSheet “noticeable delay” choosing an action
It may come as a surprise to you, but iOS does not natively support animated GIFs. If you load one via UIImage all you get is the first frame statically showing. UIImage and UIImageView do have a facility for displaying animated images, but there you can only specify a total animation duration and an array of images.
When researching about animated GIFs with iOS I found two kinds of approaches:
- Compatible with UIImageView
- High-Performance custom views for displaying GIFs, with on-demand loading and caching
There are several examples for both, Mattt Thompson’s AnimatedGIFImageSerialization being one for number 1 and Flipboard’s approach being one for number 2. Mattt even goes as far as swizzeling [UIImage imageNamed:] to add the missing support. But as far as I can tell he is only calculating the entire duration of the animation with individual frames playing at equal durations.
Rob Mayoff offers a solution for the problem of varying frame durations by finding the shortest duration that all frame durations are a multiple of. This way you can duplicate individual frames thus duplicating the frame’s duration. Rob’s approach gave me the most inspiration, although I thought the implementation to be somewhat clunky. I mixed his approach with a more elegant recursive method for finding the greatest common factor and – supposedly – the way WebKit calculates frame durations.
Approach number 1 might have some performance bottlenecks with very large images. For these kind of scenarios approach number 2 is definitely the better option. But for introducing support for playing low-weight GIFs in DTCoreText this is probably just as fine.
The update to DTFoundation is tagged on GitHub and also available via Cocoapods.