简体   繁体   中英

Show Gifs On CollectionView Swift

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...

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM