swift 在 UICollectionView 上添加 Youtube 缩略图

Adding Youtube thumbnail on UICollectionView in swift

我正在尝试找到一种方法来在 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
}

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

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
}