15

I thought iOS 11 was supposed to bring, at long last, native support for animated gifs? But I tried this, and I didn't see any animation:

let im = UIImage(named:"wireframe.gif")!
let iv = UIImageView(image:im)
iv.animationImages = [im] // didn't help
iv.frame.origin = CGPoint(0,100)
iv.frame.size = im.size
self.view.addSubview(iv)
delay(2) {
    iv.startAnimating() // nope
}

How is this supposed to work?

Komal12
  • 3,340
  • 4
  • 16
  • 25
matt
  • 515,959
  • 87
  • 875
  • 1,141

2 Answers2

18

iOS 11 does bring a native understanding of animated gifs, but that understanding, infuriatingly, is not built into UIImageView. It is still up to you to translate the animated gif into a sequence of UIImages. Apple now provides sample code, in terms of the ImageIO framework:

https://developer.apple.com/library/content/samplecode/UsingPhotosFramework/Listings/Shared_AnimatedImage_swift.html

That code implements an AnimatedImage class, which is essentially a collection of CGImages extracted from the original animated gif. Thus, using that class, we can display and animate the animated gif in a UIImageView as follows:

let url = Bundle.main.url(forResource: "wireframe", withExtension: "gif")!
let anim = AnimatedImage(url: url)!
var arr = [CGImage]()
for ix in 0..<anim.frameCount {
    arr.append(anim.imageAtIndex(index: ix)!)
}
var arr2 = arr.map {UIImage(cgImage:$0)}
let iv = UIImageView()
iv.animationImages = arr2
iv.animationDuration = anim.duration
iv.frame.origin = CGPoint(0,100)
iv.frame.size = arr2[0].size
self.view.addSubview(iv)
delay(2) {
    iv.startAnimating()
}
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 7
    That link is a 404. Has the feature been removed? – DrMickeyLauer Sep 18 '17 at 12:56
  • Ok, thanks. Very strange. If I understand that correctly, then by directly querying `ImageIO`, it has been possible to get the the individual images in an animated `.gif` since ages. I really don't know what's new in iOS 11 then. – DrMickeyLauer Sep 18 '17 at 14:27
  • @DrMickeyLauer Well, Apple has made a huge deal of what is new in iOS 11, so watch the WWDC videos. Basically what they show is that the Photos app displays animated gifs natively. And that is what my question / answer here is about: if the Photos app can display animated gifs natively, can UIImageView do it too? – matt Sep 18 '17 at 14:29
  • 5
    URL is broken again – Shane Neuville Aug 15 '18 at 21:21
  • 2
    @ShaneNeuville Yes, I noticed that too. Apple can't seem to make up its mind about this. They let the code out of the bag — thus encouraging us to do it that way — and now they've tried to stuff it back in. Basically it seems they don't want us to show animated gifs after all. – matt Aug 15 '18 at 21:23
  • @ShaneNeuville Part of the reason might be that translating an animated gif into an array of images can run your app out of memory in short order... – matt Aug 15 '18 at 21:25
  • so what's your recommendation for rendering GIFs in iOS? thanks! – Crashalot Jan 04 '19 at 02:04
  • 1
    Hi @Crashalot! — Now that Apple has withdrawn its official code, we're back to using third-party libraries (of which there are many). I think the idea here might be that animated gifs can be extremely memory-heavy and Apple just doesn't want to encourage people using them... – matt Jan 04 '19 at 03:09
  • OK thanks! So that means breaking GIFs into separate frames? Thanks again! – Crashalot Jan 04 '19 at 05:08
  • @Crashalot Yes, that’s what Apple’s code was doing too. – matt Jan 04 '19 at 05:56
  • ok thanks! and thanks again for all your contributions to stackoverflow! you have helped many people with your knowledge. – Crashalot Jan 04 '19 at 07:31
  • so we can't use the `AnimatedImage(url: url)` again? – Farras Doko May 11 '21 at 02:32
2

Unfortunately, the inter-frame timing of a GIF can vary between frames, so answers that use ImageIO to load the frames and then set them as the animatedImages on a UIImageView need to properly extract the timings and take them into account.

I recommend Flipboard's FLAnimatedImage, which handles GIFs correctly. https://github.com/Flipboard/FLAnimatedImage.

Ian Wilkinson
  • 160
  • 1
  • 3
  • I totally agree. But the Apple example code I cited also includes an AnimatedImageView class that does this correctly. The point is merely that there is no need for third-part code (except that Apple keeps putting their own code up and taking it down again, which is a bit nutty). – matt Dec 13 '17 at 18:15