【发布时间】: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