简体   繁体   English

在CollectionView Swift上显示Gif

[英]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 我的收藏夹视图不使用GIPHY ..和SwiftGif Extension显示gifs.im,而是在UIImageView上显示gifs ...这是代码

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 ..... 以及numberOfItems in sections中的numberOfItems in sections .....

I put gifModel.count the data works good, I have a response with the 6 on the array model... 我把gifModel.count数据工作得很好,我对数组模型6有一个响应...

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? 我尝试使用String,但是什么也没有,单元格已经创建,但是不显示gifs……有人可以帮忙吗?

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... 网址为nill,但在我的模型中,数据网址正确无误...

I figured out!.. GIPHY, have a struct very "inbound", I get the image Gif inside of the response like this.... 我想通了!.. GIPHY,有一个非常“入站”的结构,我在响应中得到了图像Gif,就像这样。

results.images?.original?.gifUrl results.images?.original?.gif网址

     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... 现在我可以得到带有扩展名“ .GIF”的URL,并且SwiftGif可以在集合上显示该gifs ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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