Animated and transparent GIF’s for iPhone made easy!

- Update 2012-11-14: Since people are still using AnimatedGif 3 years after I initially created this project I uploaded the source to GitHUB. Please contribute to the source from there! https://github.com/scspijker/iOS_AnimatedGif

I did not expect this, as I created this project when I left school in 2009 for an application, to prove that I could work in Objective C… I will try to do a minor (ARC!) update Soon[tm]. I will only release this on github.

- Update 2010-01-22: Major rework of the module, making stuff even easier, and having better memory management..

I recently started developing for iPhone, and I was looking to include some simple animations into my applications, like a typical AJAX loading GIF when I’m waiting for my stream of data to come in.
It surprised me that nothing was available! After a lot of searching I found a block of code by Martin van Spanje from P-Edge. It was based on some PHP code, splitting an animated GIF in subframes.

The code worked, Martin stated it needed to be checked and optimized. So I went to work. I upped the code to the GIF 89a standards, I created a class out of it. Best of all, I got animation working WITH transparency! I believe in Apple’s idea of simplicity, so I made a set of interfaces that -anyone- should be able to use. Here’s what you need to open up a GIF, and set it up to animate in your project:

    // First example, a local file
    NSURL* firstUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"apple_logo_animated" ofType:@"gif"]];
    UIImageView * firstAnimation = [AnimatedGif getAnimationForGifAtUrl: firstUrl];
 
    // Second example, through HTTP
    NSURL * secondUrl = [NSURL URLWithString:@"http://www.gifs.net/Animation11/Food_and_Drinks/Fruits/Apple_jumps.gif"];
    UIImageView * secondAnimation = [AnimatedGif getAnimationForGifAtUrl: secondUrl];
 
    // Add them to the view.
    [theFirstAnimatedGif addSubview:firstAnimation];
    [theSecondAnimatedGif addSubview:secondAnimation];

Best of all, I’m releasing this FREE for commercial use as well, as long as you keep the permission notice in the source files!
The download consists of the AnimatedGif class (.h and .m files), and the code should be self-explanatory. Should you require any help: leave a comment, or mail me! (email in example project result, or in my Curriculum Vitea)

Download just the class:
[download id="3"]

Download an example XCode 3.1.3 project
[download id="4"]

//  Changelog:
//
//  2010-03-16: Added queing mechanism for static class use
//  2010-01-24: Rework of the entire module, adding static methods, better memory management and URL asynchronous loading
//  2009-10-08: Added dealloc method, and removed leaks, by Pedro Silva
//  2009-08-10: Fixed double release for array, by Christian Garbers
//  2009-06-05: Initial Version

55 thoughts on “Animated and transparent GIF’s for iPhone made easy!

  1. I’m using primarily the decodeGIF: and getFrameAsImageAtIndex: methods, and those methods work great. Thanks for making this code available.

    There are several warnings and potential memory leaks shown in Xcode when compiling. To repair them, this is the diff of what needs to be done:

    https://gist.github.com/3618719

    The networking code is not truly asynchronous, and will lock the main thread.

    Again, thank you for making this code available. It is very useful.

Comments are closed.