【问题标题】:Get thumbnails for Array of Videos获取视频数组的缩略图
【发布时间】: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


【解决方案1】:

试试下面的代码:

//Get the document directory path
    let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
            let videoFolderPath = documentDirectoryPath.appending("/Videos/")

//Generate the document directory enumerator
let videos = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last!.appendingPathComponent("Videos")
        let videoFilesEnumerator = FileManager.default.enumerator(atPath: videos.path).   //This will contain all the files in the videos folder

使用以下函数从“视频”文件夹中的视频生成图像

func generateThumbnail(path: URL) -> UIImage? {
        do {
            let asset = AVURLAsset(url: path, options: nil)
            let imgGenerator = AVAssetImageGenerator(asset: asset)
            imgGenerator.appliesPreferredTrackTransform = true
            let cgImage = try imgGenerator.copyCGImage(at: CMTimeMake(value: 0, timescale: 1), actualTime: nil)
            let thumbnail = UIImage(cgImage: cgImage)
            return thumbnail
        } catch let error {
            print("*** Error generating thumbnail: \(error.localizedDescription)")
            return nil
        }
    }

您可以将图像数组作为属性(此处为“thumbImages”)并按如下方式填充数组:

while let element = videoFilesEnumerator?.nextObject() as? String {
     let thumbImage = self.generateThumbnail(path: videos.appendingPathComponent(element))
     thumbImages.append(newImage)
}

您必须根据场景的要求处理代码中的可选值。获取所有缩略图后,您可以直接将图像设置为 tableview 中的单元格。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-25
    • 2013-08-31
    • 2017-10-21
    • 2011-05-12
    • 2011-06-18
    • 1970-01-01
    • 2020-08-27
    • 2010-11-19
    相关资源
    最近更新 更多