Support for GIF advertising

The following guide shows an example about how to load animated GIF by UIImageView for users who want to display GIF ads.

  • Also some libraries like SwiftGif can display animated GIF ads.

Identifying GIF advertisement

Loading data from Customized In-Feed advertisement parameters’s property’,can identify data is GIF if mimeType from response data is image/gif.

        URLSession.shared.dataTask(with: URLRequest(url: instreamInfoModel.imageURL!)) { (data, response, error) in
            guard let data = data, let response = response as? HTTPURLResponse, error == nil else {
                return
            }
            DispatchQueue.main.async(execute: {
                if (response.mimeType == "image/gif") {
                    //this is a gif image
                } else {
                }
            })
            }.resume()

Displaying GIF advertisement

Refering to Image I/0 framework,get imageSource from data and use animationImages to play GIF。 The following guide shows an example.

    //add imageView as a subView and call imageView's startAnimating() method
    guard let imageView = self.createGifImageFromData(data: data as NSData, bounds: yourView.containerView.bounds) else {return}
    yourView.containerView.addSubview(imageView)
    imageView.startAnimating()

    func createGifImageFromData (data : NSData, bounds : CGRect) -> UIImageView?  {
        guard let gifImagesSource = CGImageSourceCreateWithData(data as CFData, nil) else {return nil}
        //gif image's count
        let gifImagesCount = CGImageSourceGetCount(gifImagesSource)
        var gifImagesArray = [UIImage]()
        var totalDuration : TimeInterval = 0
        let imageView = UIImageView(frame: bounds)

        for i in 0 ..< gifImagesCount {
            guard let cgImage = CGImageSourceCreateImageAtIndex(gifImagesSource, i, nil) else { continue }
            let image = UIImage(cgImage: cgImage)
            if i == 0 {
                imageView.image = image
            }
            gifImagesArray.append(image)

            // Frame default duration
            var frameDuration : Float = 0.1;
            guard let properties = CGImageSourceCopyPropertiesAtIndex(gifImagesSource, i, nil) else {
                totalDuration += frameDuration
                continue }
            guard let gifDict = (properties as NSDictionary)[kCGImagePropertyGIFDictionary] as? NSDictionary else {
                totalDuration += frameDuration
                continue }
            if let delayTimeUnclampedProp = gifDict[kCGImagePropertyGIFUnclampedDelayTime as String] as? NSNumber {
                frameDuration = delayTimeUnclampedProp.floatValue
            } else {
                if let delayTimeProp = gifDict[kCGImagePropertyGIFDelayTime as String] as? NSNumber {
                    frameDuration = delayTimeProp.floatValue
                }
            }
            // Make sure its not too small
            if frameDuration < 0.01 {
                frameDuration = 0.1;
            }
            totalDuration += frameDuration
        }
        imageView.animationImages = gifImagesArray
        // Each frame has same delay time
        imageView.animationDuration = TimeInterval(totalDuration)
        // Loop gif
        imageView.animationRepeatCount = 0
        imageView.contentMode = .scaleAspectFit
        imageView.image = UIImage(data: data as Data)
        return imageView
    }
}