Cómo cargar la imagen GIF en Swift?

Tengo una cadena con una URL del banner GIF que debo poner en la aplicación.

Mi código:

func showAdd(){ Request.get("http://www.kyst.no/api/?apiMode=advertisement&lang=no", { (error: NSError?, data: NSData, text: NSString?) -> () in let jsonResult: Dictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as Dictionary var banner : NSString = jsonResult["advertisement"]!["banner"] as NSString self.addViewImage.image = UIImage.animatedImageNamed(banner, duration: 1) }) } 

Pero nada pasa. Por favor ayuda.

Cargar imagen GIF Swift:

# 1: Copie el archivo rápido de este enlace :

# 2: Cargar imagen GIF usando nombre

  let jeremyGif = UIImage.gifImageWithName("funny") let imageView = UIImageView(image: jeremyGif) imageView.frame = CGRect(x: 20.0, y: 50.0, width: self.view.frame.size.width - 40, height: 150.0) view.addSubview(imageView) 

# 3: Cargar imagen GIF usando datos

  let imageData = try? Data(contentsOf: Bundle.main.url(forResource: "play", withExtension: "gif")!) let advTimeGif = UIImage.gifImageWithData(imageData!) let imageView2 = UIImageView(image: advTimeGif) imageView2.frame = CGRect(x: 20.0, y: 220.0, width: self.view.frame.size.width - 40, height: 150.0) view.addSubview(imageView2) 

# 4: Cargar imagen GIF usando URL

  let gifURL : String = "http://www.gifbin.com/bin/4802swswsw04.gif" let imageURL = UIImage.gifImageWithURL(gifURL) let imageView3 = UIImageView(image: imageURL) imageView3.frame = CGRect(x: 20.0, y: 390.0, width: self.view.frame.size.width - 40, height: 150.0) view.addSubview(imageView3) 

Descargar código de demo

SALIDA:

iPhone 8 / iOS 11 / xCode 9

enter image description here

 // // iOSDevCenters+GIF.swift // GIF-Swift // // Created by iOSDevCenters on 11/12/15. // Copyright © 2016 iOSDevCenters. All rights reserved. // import UIKit import ImageIO extension UIImage { public class func gifImageWithData(data: NSData) -> UIImage? { guard let source = CGImageSourceCreateWithData(data, nil) else { print("image doesn't exist") return nil } return UIImage.animatedImageWithSource(source: source) } public class func gifImageWithURL(gifUrl:String) -> UIImage? { guard let bundleURL = NSURL(string: gifUrl) else { print("image named \"\(gifUrl)\" doesn't exist") return nil } guard let imageData = NSData(contentsOf: bundleURL as URL) else { print("image named \"\(gifUrl)\" into NSData") return nil } return gifImageWithData(data: imageData) } public class func gifImageWithName(name: String) -> UIImage? { guard let bundleURL = Bundle.main .url(forResource: name, withExtension: "gif") else { print("SwiftGif: This image named \"\(name)\" does not exist") return nil } guard let imageData = NSData(contentsOf: bundleURL) else { print("SwiftGif: Cannot turn image named \"\(name)\" into NSData") return nil } return gifImageWithData(data: imageData) } class func delayForImageAtIndex(index: Int, source: CGImageSource!) -> Double { var delay = 0.1 let cfProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil) let gifProperties: CFDictionary = unsafeBitCast(CFDictionaryGetValue(cfProperties, Unmanaged.passUnretained(kCGImagePropertyGIFDictionary).toOpaque()), to: CFDictionary.self) var delayObject: AnyObject = unsafeBitCast(CFDictionaryGetValue(gifProperties, Unmanaged.passUnretained(kCGImagePropertyGIFUnclampedDelayTime).toOpaque()), to: AnyObject.self) if delayObject.doubleValue == 0 { delayObject = unsafeBitCast(CFDictionaryGetValue(gifProperties, Unmanaged.passUnretained(kCGImagePropertyGIFDelayTime).toOpaque()), to: AnyObject.self) } delay = delayObject as! Double if delay < 0.1 { delay = 0.1 } return delay } class func gcdForPair(a: Int?, _ b: Int?) -> Int { var a = a var b = b if b == nil || a == nil { if b != nil { return b! } else if a != nil { return a! } else { return 0 } } if a! < b! { let c = a! a = b! b = c } var rest: Int while true { rest = a! % b! if rest == 0 { return b! } else { a = b! b = rest } } } class func gcdForArray(array: Array) -> Int { if array.isEmpty { return 1 } var gcd = array[0] for val in array { gcd = UIImage.gcdForPair(a: val, gcd) } return gcd } class func animatedImageWithSource(source: CGImageSource) -> UIImage? { let count = CGImageSourceGetCount(source) var images = [CGImage]() var delays = [Int]() for i in 0.. 

Aquí está el archivo actualizado para Swift 3

Primero instala un pod: –

 pod 'SwiftGifOrigin' 

e importa en tu clase

 import SwiftGifOrigin 

luego escribe este código en el método viewDidiload

 yourImageView.image = UIImage.gif(name: "imageName") 

Nota: – Por favor, no incluya la extensión de archivo en el nombre de archivo gif. Ex:-

 //Don't Do this yourImageView.image = UIImage.gif(name: "imageName.gif")