1

my collection view don't show gifs.. im using GIPHY.. and SwiftGif Extension, to show the gifs on UIImageView... this is the code

func searchGif(search: String) {
    GiphyCore.configure(apiKey: "hRuR15WOxvhonLAsLhd0R8pDGvJxQYOk")
    respondView.isHidden = true
    _ = GiphyCore.shared.search(search, media: .gif, offset: 2, limit: 6, rating: .ratedG, lang: .english, completionHandler: { [weak self] (response, error) in
        self?.isDataLoading = false
        if let error = error {
            print("error in response", error)
        }
        guard
            let data = response?.data else { return }
        self?.initialize()
        for results in data {
            let urlString = results.url
            guard let url = URL(string: urlString) else { return }
            do {
                let data = try Data(contentsOf: url)
                let foundedGif = GifModel(gifUrl: data, urlString: urlString)
                self?.gifModel.append(foundedGif)
            } catch let error as NSError {
                print(error)
            }
        }
        if self?.gifModel.isEmpty ?? false {
            self?.setupNofound()
        }
        DispatchQueue.main.async {
            self?.gifCollectionView.reloadData()
        }
    })
}

in the delegates on collection view...

func collectionView(_ collectionView: UICollectionView,
                    cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    guard let cell = collectionView.dequeueReusableCell(
        withReuseIdentifier: GifSearchCollectionViewCell.identifier,
        for: indexPath
        ) as? GifSearchCollectionViewCell else {
            return UICollectionViewCell()
    }
    cell.gifModel = gifModel[indexPath.row]
    return cell
}

and in the numberOfItems in sections as well .....

I put gifModel.count the data works good, I have a response with the 6 on the array model...

and in the cell:

@IBOutlet weak var splashGifView: UIImageView!
var gifModel: GifModel? {
    didSet {
        guard
            let gif = gifModel?.gifUrl else { return }
        splashGifView.image = UIImage.gifImageWithData(gif)
    }
}

I tried with String but nothing, the cells already create, but don't show the gifs... someone can help?

update...

       @IBOutlet weak var splashGifView: UIImageView!
var gifModel: GifModel? {
    didSet {
        guard let gif = gifModel? { return }
        let url = gif.gifUrl. // <- this give nill
        splashGifView.image = UIImage.gifImageWithData(url)  
    }
}

the url have nill, but in my model I have the data url correctly...

  • Try this: https://stackoverflow.com/questions/27919620/how-to-load-gif-image-in-swift – MagnunStalin Sep 18 '18 at 21:01
  • Try this Pod: https://github.com/kaishin/Gifu this pod it's very easy to use and fast loading – Magdy Zamel Sep 19 '18 at 00:32
  • Could you clarify: Does `splashGifView.image = UIImage.gifImageWithData(gif)` works if you put another URL? What is strange, is that you call `gifImageWithData()`, with that method name, I'd expect a `(NS)Data` param, not a `URL` one. Are you using the correct method? Do you have a warning? Could you give the link of your Pod/Extension you are using for the gif? – Larme Sep 19 '18 at 10:14
  • Yes I Use SwiftGif extension for UIImageView, here you can see how works ... https://github.com/swiftgif/SwiftGif, when I have local gifs it works perfectly, but I try get url gifs, using Giphy pod, this one give, the urls of the gifs I get 6 per request, but using the methods of this extension, (gifWithImageData.. & ... gifWithUrl()...) doesn't show in my CollectionVIew, I try using KingFisher to, but I thing there something missing in my code, don't know what it is... – Yan Cervantes Sep 19 '18 at 16:33
  • I try this... let image = UIImage.gifImageWithURL(url), and I set... gifImageView.image = image... and I put a break point to see what is inside on image.. but is nill.... someone can help? – Yan Cervantes Sep 19 '18 at 17:56
  • So in fact `let url = gif.gifUrl` is `nil`. So it didn't work because you didn't pass the `guard let gif = gifModel?.gifUrl else { return }` test, nothing to do with `SwiftGif`. Issue is `let data = try Data(contentsOf: url); let foundedGif = GifModel(gifUrl: data, urlString: urlString)` then. It has flaws, why downloading the content ? Synchronously !?! Is it supposed to be an URL and not a Data? – Larme Sep 20 '18 at 09:31
  • I guess first need download the data, because only show me the URL, in type string, not like Gif, and UIImageView, never recognize that.... and that URL send a page.. not the GIF, I will try download the data, to pass like an image I guess, or how I can download data? – Yan Cervantes Sep 20 '18 at 15:08

1 Answers1

0

I figured out!.. GIPHY, have a struct very "inbound", I get the image Gif inside of the response like this....

results.images?.original?.gifUrl

     for results in data {
           let newGif = GifModel(gifUrl: results.images?.original?.gifUrl ?? "", run: false)     
           self?.gifModel.append(newGif)
     }

and now I can get the url with the extension ".GIF" and with that SwiftGif can show on the collectionView the gifs...