【问题标题】:Adding Youtube thumbnail on UICollectionView in swift快速在 UICollectionView 上添加 Youtube 缩略图
【发布时间】:2015-09-13 16:36:43
【问题描述】:

我正在尝试找到一种在UICollectionView 上添加一组 youtube 视频缩略图的方法。但是在我尝试缩略图(图像)的方式上,没有显示出来,而且我还没有找到在NSData 上添加 URL 集合(数组)的方法。

或者如果有其他方法可以做到这一点,比如使用 Youtube API。

顺便提一下,我不了解 Obj-C,所以 Obj-C 中的任何代码都不会很有帮助。

这是我的代码

var videoName:[String] = ["First Video", "Second Video", "Third Video"]
var videoImage:[String] = ["thumbnail1","thumbnail2","thumbnail3"]

let thumbnail1 = NSURL(string: "https://www.youtube.com/watch?v=sGF6bOi1NfA/0.jpg")
let thumbnail2 = NSURL(string: "https://www.youtube.com/watch?v=y71r1jhMdRk/0.jpg")
let thumbnail3 = NSURL(string: "https://www.youtube.com/watch?v=qfsRZsvraD8/0.jpg")

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: SongCollectionView = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! SongCollectionView

    cell.labelCell.text = videoName[indexPath.row]
    let imageData = NSData(contentsOfURL: /*videoImage[indexPath.row]*/ thumbnail1!) // in the commented section I am trying to add an array of strings which contains thumbnail URL's
    cell.imageCell.image = UIImage(data: imageData!)
    return cell
}

【问题讨论】:

  • https://www.youtube.com/watch?v=sGF6bOi1NfA/0.jpg 重定向到视频,并且不显示缩略图。您确定您使用的网址正确吗?
  • 是的,你是对的,这是一个愚蠢的错误 thnx
  • 请问正确的网址是什么?

标签: swift youtube uicollectionview thumbnails youtube-data-api


【解决方案1】:

使用连音而不是两个单独的数组的解决方案

let videoData = [("First Video", "sGF6bOi1NfA/0"), ("Second Video","y71r1jhMdRk/0"), ("Third Video", "qfsRZsvraD8/0")]

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
  let cell: SongCollectionView = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! SongCollectionView
  let (name, token) = videoData[indexPath.row]

  cell.labelCell.text = name
  let imageData = NSData(contentsOfURL: NSURL(string: "https://www.youtube.com/watch?v=\(token).jpg")!)
  cell.imageCell.image = UIImage(data: imageData)
  return cell
}

【讨论】:

  • 只是想问你另一个问题。有没有更好的方法(例如不手动添加视频 ID,或取视频名称等)?
  • 这取决于你要完成什么。例如,您还可以创建一个包含名称和令牌属性的结构,并在可变数组中添加、更新或删除实例
猜你喜欢
  • 1970-01-01
  • 2013-04-03
  • 2020-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-04
相关资源
最近更新 更多