【发布时间】:2019-10-26 13:55:46
【问题描述】:
我找到了几种使用AVAsset 获取视频缩略图的方法,但是我想获取一系列视频的缩略图。我正在从 iPad 上的应用程序文档文件夹中填充数组。这就是我获取数组的方式:
func listVideoFiles(){
let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
let videoFolderPath = documentDirectoryPath.appending("/Videos")
let videoFiles = FileManager.default.enumerator(atPath: videoFolderPath)
while let file = videoFiles?.nextObject() {
videoArray.append("\(file)")
print("")
}
if videoFiles == nil {
videoArray.append("No Videos Found")
print("No Files")
}
}
我在 collectionView 中显示数组,如下所示:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cellA = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! VideoViewCell
cellA.VideoCellLabel.text = videoArray[indexPath.item]
gradientLayer = CAGradientLayer()
gradientLayer.frame = cellA.contentView.bounds
gradientLayer.colors = [#colorLiteral(red: 0.5137254902, green: 0.3764705882, blue: 0.7647058824, alpha: 1).cgColor, #colorLiteral(red: 0.1803921569, green: 0.7490196078, blue: 0.568627451, alpha: 1).cgColor]
cellA.contentView.layer.insertSublayer(gradientLayer, at: 0)
return cellA
}
如何获取每个视频的缩略图并将其显示在相应的单元格中?
【问题讨论】:
-
我只是想让您知道,如果您在“cellForItemAt”中插入子图层,那么每次重新加载单元格时都会插入一个图层,这对我来说不是很好。仅在单元格级别创建一个图层,而不是“cellForItemAt”。
-
感谢您的建议,有什么更好的方法呢?一旦缩略图正常工作,我实际上不会添加子图层。每个单元格都有一个图像背景而不是渐变
标签: swift ipad video uicollectionview avasset