Search code examples
iosswiftuitableviewgifanimated-gif

Loading .gif in tableView


I have an app with 3 views, 2 of them are tableViews (lists) and on the third view Im trying load gifs through passing data (text and gifs), different for each item in the list.

enter image description here

I have found an extension to play gifs called SwiftGif, which works great when using in ordinary viewControllers, but the gif is not moving when passing data between tableViews.

SwiftGif contains function:

extension UIImageView {

public func loadGif(name: String) {
    DispatchQueue.global().async {
        let image = UIImage.gif(name: name)
        DispatchQueue.main.async {
            self.image = image
        }
    }
}

And my ThirdView.swift file contains:

import Foundation
import UIKit
import ImageIO

let name = String()
var gifs =  UIImage.gif(name: name)


struct IngredientsAndDirections {
  var ThirdViewArray = [String]()
  var Pic = [gifs]
}

But I can't figure out how to call the loadGif function on the array of gifs to make my gifs moving.

Any suggestions please?


Solution

  • In case anyone is looking for a solution (Swift 4).

    1. I implemented library https://github.com/swiftgif/SwiftGif
    2. Created arrays with Name and Images (var name: NSArray = [], var images: NSArray = []
    3. In viewDidLoad inserted all of the arrays (name = ["", "", ""] and images = ["", "", ""] -> the count must be the same)
    4. Created TableViewCell.swift file, inserted all of the data
    5. Through passing data everything works as a charm, the gifs even move in tableViews