【发布时间】: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