【问题标题】:Show Gifs On CollectionView Swift在 CollectionView Swift 上显示 Gif
【发布时间】:2019-02-22 22:58:17
【问题描述】:

我的收藏视图不显示 gifs.. 我正在使用 GIPHY.. 和 SwiftGif Extension,在 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()
        }
    })
}

在集合视图的代表中...

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
}

numberOfItems in sections 中也是如此.....

我把gifModel.count的数据很好用,数组模型上的6我有回应...

在单元格中:

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

我尝试使用 String 但没有,单元格已经创建,但不显示 gifs...有人可以帮忙吗?

更新...

       @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)  
    }
}

网址没有,但在我的模型中,我的数据网址正确...

【问题讨论】:

  • 试试这个 Pod:github.com/kaishin/Gifu 这个 Pod 它非常易于使用和快速加载
  • 您能否澄清一下:如果您输入另一个 URL,splashGifView.image = UIImage.gifImageWithData(gif) 是否有效?奇怪的是,您使用该方法名称调用gifImageWithData(),我希望是(NS)Data 参数,而不是URL。您是否使用正确的方法?你有警告吗?您能否提供用于 gif 的 Pod/Extension 的链接?
  • 是的,我使用 UIImageView 的 SwiftGif 扩展,在这里你可以看到它是如何工作的...github.com/swiftgif/SwiftGif,当我有本地 gif 时它工作得很好,但我尝试使用 Giphy pod 获取 url gif,这个一个给,每个请求我得到 6 个 gif 的 URL,但是使用这个扩展的方法,(gifWithImageData.. & ... gifWithUrl()...) 没有显示在我的 CollectionVIew 中,我尝试使用 KingFisher ,但我的代码中缺少一些东西,不知道它是什么......
  • 我试试这个...让 image = UIImage.gifImageWithURL(url),然后我设置... gifImageView.image = image... 我放了一个断点,看看里面是什么图片..但是没有....有人可以帮忙吗?

标签: ios swift uicollectionview gif


【解决方案1】:

我想通了!.. GIPHY,有一个非常“入站”的结构,我在这样的响应中得到图像 Gif....

results.images?.original?.gifUrl

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

现在我可以获取带有扩展名“.GIF”的 url,并且 SwiftGif 可以在 collectionView 上显示 gifs...

【讨论】:

    猜你喜欢
    • 2017-07-01
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 2014-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多