New animated GIF decoder for AVAnimator

June 12, 2013

The AVAnimator library for iOS makes it easy to implement non-trivial video in an iPhone or iPad app. Supported video formats include APNG, MOV (h.264), and the custom MVID file format built specifically for iOS. But, one older format that is not supported directly is an animated GIF, like this one:


superwalk

The animated GIF format is quite limited in that is supports only a palette of 256 colors and a 1 bit transparency. But, quite a few animated GIFs can be found online and a developer may want to include this type of media in an iOS app. For example, in a forum type of application the user could upload an avatar using an animated GIF.

Support for animated GIFs has now been added to AVAnimator via a new loader named AVGIF89A2MvidResourceLoader. The code will be included in the next release, is currently available on github:


A motivated developer could grab the new loader .h and .m file and include it in an existing project that already includes AVAnimator.

The loader module will read a GIF 89a file using the ImageIO Framework provided by iOS. A previous post covers memory issues under iOS and the mistakes made in the other iOS GIF decoders found online. This GIF decoder implementation will not use up all app memory and crash your app when presented with a large GIF.

The loader creates a secondary thread to decode a GIF from the app resources or from a regular file. The loader object will write a new .mvid file that contains either 24BPP or 32BPP pixels depending on if a transparent pixel was used in the GIF.

The Objective-C code needed to create this type of loader works the same way as any other AVAnimator media loader object:

// Create loader that will load .mvid from
// a .gif attached as a project resource

NSString *resFilename = @"superwalk.gif";
NSString *tmpFilename = @"superwalk.mvid";

AVGIF89A2MvidResourceLoader *resLoader =
[AVGIF89A2MvidResourceLoader aVGIF89A2MvidResourceLoader];

NSString *tmpPath = [AVFileUtil getTmpDirPath:tmpFilename];
resLoader.movieFilename = resFilename;
resLoader.outPath = tmpPath;

// Create Media object

AVAnimatorMedia *media = [AVAnimatorMedia aVAnimatorMedia];

media.resourceLoader = resLoader;
media.frameDecoder =
  [AVMvidFrameDecoder aVMvidFrameDecoder];

That is all there is to it! With this new loader module, an animated GIF can now be used as a source of video content in AVAnimator. The high performance video blit and advanced memory management logic in AVAnimator will be used with video content defined as an animated GIF.