SWIFT 3
Here is an update for those who need the Swift version!
A few days ago I needed to do something like this. I download some data from the server according to certain parameters, and in the meantime I wanted to show another gif image "download". I was looking for an option to do this using UIImageView , but unfortunately I could not find something for this without breaking the .gif image. So I decided to implement the solution using UIWebView , and I want to share it:
extension UIView{ func animateWithGIF(name: String){ let htmlString: String = "<!DOCTYPE html><html><head><title></title></head>" + "<body style=\"background-color: transparent;\">" + "<img src=\""+name+"\" align=\"middle\" style=\"width:100%;height:100%;\">" + "</body>" + "</html>" let path: NSString = Bundle.main.bundlePath as NSString let baseURL: URL = URL(fileURLWithPath: path as String) // to load images just specifying its name without full path let frame = CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height) let gifView = UIWebView(frame: frame) gifView.isOpaque = false // The drawing system composites the view normally with other content. gifView.backgroundColor = UIColor.clear gifView.loadHTMLString(htmlString, baseURL: baseURL) var s: [UIView] = self.subviews for i in 0 ..< s.count { if s[i].isKind(of: UIWebView.self) { s[i].removeFromSuperview() } } self.addSubview(gifView) } func animateWithGIF(url: String){ self.animateWithGIF(name: url) } }
I made an extension for UIView that adds a UIWebView as a subview and displays .gif images by simply passing its name.
Now in my UIViewController I have a UIView called "loadView", which is my loading indicator and whenever I wanted to show a .gif image, I did something like this:
class ViewController: UIViewController { @IBOutlet var loadingView: UIView! override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) configureLoadingView(name: "loading.gif") } override func viewDidLoad() { super.viewDidLoad()
when I wanted to change the gif image, I just called the configureLoadingView() function the name of my new .gif image and by calling showLoadingView() , hideLoadingView() everything worked fine fine!
BUT...
... if you split the image, you can animate it on one line using the static UIImage method called UIImage.animatedImageNamed as follows:
imageView.image = UIImage.animatedImageNamed("imageName", duration: 1.0)
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. All images included in the animated image must be the same size and scale.
Or you can do this using the UIImage.animatedImageWithImages method as follows:
let images: [UIImage] = [UIImage(named: "imageName1")!, UIImage(named: "imageName2")!, ..., UIImage(named: "imageNameN")!] imageView.image = UIImage.animatedImage(with: images, duration: 1.0)
From the docs:
Creates and returns an animated image from an existing set of images. All images included in the animated image must be the same size and scale.