从文档中的视频中获取缩略图的问题

时间:2017-08-06 08:42:08

标签: ios iphone swift uiimage avasset

我正在尝试从存储在文档目录中的视频文件生成缩略图。首先,我只收集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。

2 个答案:

答案 0 :(得分:0)

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

以下是我测试的代码。

//validate args , not null, and length != 0

if(args == null || args.length ==0){
    System.out.println("No strings to compare");
    return;
}
string lesser = args[0]; // assume 1st argument is the lesser.
for(int a=0;a<args.length;a++){
    if (lesser.compareTo(args[a]) > 0) {
        lesser = args[a];// the new lesser string
    }
    //we ignore other cases (args[a] is larger or equals, cuz we want lesser only
}

System.out.println("Lesser String is " + lesser);

答案 1 :(得分:0)

最后通过找到正确的URL

来解决问题
let documentsURL = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!,isDirectory: true ) 

let urlToMyPath = documentsURL.appendingPathComponent(fileURL.lastPathComponent.removingPercentEncoding!)!