Ios 从文档中的视频获取缩略图时出现问题
我试图从存储在文档目录中的视频文件生成缩略图。首先,我只收集Ios 从文档中的视频获取缩略图时出现问题,ios,iphone,swift,uiimage,avasset,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(fil
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
}
}
我正在从以下视频生成缩略图:
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
@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
,解决问题:
let documentsURL = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!,isDirectory: true )
let urlToMyPath = documentsURL.appendingPathComponent(fileURL.lastPathComponent.removingPercentEncoding!)!
你的url路径是什么?你能把它粘贴到这里吗?下面是我在模拟器中检查的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…
检查了此项?我需要它作为文档目录。”