Display animated GIF in iOS

I noticed that with iMessage you can now send and display animated gifs. Does this mean that Apple now supports displaying animated GIFs in the application, or is it the easiest way to split the image in frames and then display them sequentially? What is the easiest way to display an animated GIF with iOS 5.1?

Thank!

+80
ios objective-c
Mar 16 '12 at 21:21
source share
6 answers

If you are targeting iOS7 and already have an image broken into frames, you can use animatedImageNamed:duration:

Say you revive a spinner. Copy all your frames into the project and name them as follows:

  • spinner-1.png
  • spinner-2.png
  • spinner-3.png
  • etc.,

Then create an image using:

 [UIImage animatedImageNamed:@"spinner-" duration:1.0f]; 

From the docs :

This method loads a series of files by adding a series of numbers to the base file name specified in the name parameter. For example, if the name parameter had "image as its contents", this method try to load images from files named image0, image1, and so on up to 'image1024. All images included in the animated image must have the same size and scale.

+130
Feb 20 '14 at 15:08
source share

I would recommend using the following code, which is much lighter and more compatible with ARC and non-ARC projects, adds a simple category to UIImageView:

https://github.com/mayoff/uiimage-from-animated-gif/

+37
Jan 19 '13 at 14:43
source share

FLAnimatedImage is a powerful open source animated GIF engine for iOS:

  • Play multiple GIFs at the same time as desktop browsers
  • Marks variable frame delays
  • Works well under memory pressure
  • Eliminates delays or locks during the first playback cycle.
  • Interprets the frame delays of fast GIFs in the same way as modern browsers.

This is a well-tested component that I wrote in to include all GIFs in Flipboard .

+31
Aug 08 '14 at 5:15
source share

Another alternative is to use UIWebView to display an animated GIF. If the GIF is received from the server, then this will take care of the extraction. It also works with local GIFs.

+12
Apr 7 '14 at 9:14
source share
 #import <QuickLook/QuickLook.h> #import "ViewController.h" @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; QLPreviewController *preview = [[QLPreviewController alloc] init]; preview.dataSource = self; [self addChildViewController:preview]; [self.view addSubview:preview.view]; } #pragma mark - QLPreviewControllerDataSource - (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)previewController { return 1; } - (id)previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)idx { NSURL *fileURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"myanimated.gif" ofType:nil]]; return fileURL; } @end 
+7
Oct 08 '13 at 17:14
source share

From the iOS 11 Photo framework, you can add animated Gif files.

Sample application can be downloaded here.

More on animating Gifs (starting at 13:35 min.): Https://developer.apple.com/videos/play/wwdc2017/505/

enter image description here

+2
Aug 11 '17 at 11:58 on
source share



All Articles