【问题标题】:Issue with getting thumbnail from videos in documents从文档中的视频获取缩略图的问题
【发布时间】:2018-01-13 18:34:24
【问题描述】:

我正在尝试从存储在文档目录中的视频文件生成缩略图。首先,我只收集mp4 格式的视频文件:

 override func viewWillAppear(_ animated: Bool) {

        let VIDEO = [collect(files: "mp4"),
                     collect(files: "MP4")]
        videoArray = Array(VIDEO.joined())

    }


 func collect(files:String) -> [String] {

        var fileExtentions = [String]()

        let documentsUrl =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!

        let fileManager = FileManager.default
        let keys = [URLResourceKey.isDirectoryKey, URLResourceKey.localizedNameKey]
        let options: FileManager.DirectoryEnumerationOptions = [.skipsPackageDescendants, .skipsSubdirectoryDescendants, .skipsHiddenFiles]

        let enumerator = fileManager.enumerator(
            at: documentsUrl,
            includingPropertiesForKeys: keys,
            options: options,
            errorHandler: {(url, error) -> Bool in
                return true
        })


        if enumerator != nil {
            while let file = enumerator!.nextObject() {
                let path = URL(fileURLWithPath: (file as! URL).absoluteString, relativeTo: documentsUrl).path
                if path.hasSuffix(files){
                    fileExtentions.append(path)
                }
            }
        }


        return fileExtentions
    }

然后像这样填充tableview的数据:

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! VideoCell

        let files = videoArray[indexPath.row]
        let fileURL = URL(fileURLWithPath: files)

        //Vido Title
        cell.videoTitle.text = fileURL.lastPathComponent.removingPercentEncoding!


        cell.thumbnail.image = getThumbnailFrom(path:fileURL)

        return cell
    }

我正在从这样的视频中生成缩略图:

  func getThumbnailFrom(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(0, 5), actualTime: nil)
            let thumbnail = UIImage(cgImage: cgImage)

            return thumbnail

        } catch let error {

            print("*** Error generating thumbnail: \(error.localizedDescription)")
            return nil

        }

    }

但最后在运行应用程序时,编译器给了我这个错误:

*** 生成缩略图时出错:在此服务器上找不到请求的 URL。

【问题讨论】:

    标签: ios iphone swift uiimage avasset


    【解决方案1】:

    最后找到正确的URL来解决问题:

    let documentsURL = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!,isDirectory: true ) 
    
    let urlToMyPath = documentsURL.appendingPathComponent(fileURL.lastPathComponent.removingPercentEncoding!)!
    

    【讨论】:

      【解决方案2】:

      @Mc.Lover 从我这边测试了 getThumbnailFrom() 函数。它在我身边工作得很好。可能您可能需要检查路径 url 是否正确。否则,您拥有的视频文件集可能存在问题。

      下面是我测试的代码。

      func getThumbnailFrom() -> UIImage? {
      
          do {
              let path = Bundle.main.path(forResource: "Video_05", ofType:"mp4")!
              let asset = AVURLAsset(url: URL.init(fileURLWithPath: path) , options: nil)
              let imgGenerator = AVAssetImageGenerator(asset: asset)
              imgGenerator.appliesPreferredTrackTransform = true
              let cgImage = try imgGenerator.copyCGImage(at: CMTimeMake(0, 5), actualTime: nil)
              let thumbnail = UIImage(cgImage: cgImage)
      
              return thumbnail
      
          } catch let error {
      
              print("*** Error generating thumbnail: \(error.localizedDescription)")
              return nil
      
          }
      
      }
      

      【讨论】:

      • 你的 url 路径是什么?可以贴在这里吗?
      • 下面是我在模拟器中检查的网址。 "file:///Users/shreekara/Library/Developer/CoreSimulator/Devices/7E7EF505-E943-40FB-97EB-08EF91B0F7C4/data/Containers/Bundle/Application/589A03B8-A487-4986-888B-69ABE26CF5AE/TextDisplay.app/ Video_05.mp4"
      • 你用Bundle.main.pathFor...检查过这个?
      • 文档目录需要它
      猜你喜欢
      • 2011-05-25
      • 2020-06-26
      • 2013-08-31
      • 2017-10-21
      • 2020-12-08
      • 2013-07-22
      • 2018-11-06
      • 1970-01-01
      • 2016-03-20
      相关资源
      最近更新 更多