我目前正在开发一个小型 swift 应用程序,并将一些视频记录存储在应用程序的文档文件夹中。我想稍后再取回这些。我已经得到了一个文件位置数组,如下所示:
file:///private/var/mobile/Containers/Data/Application/6C462C4E-05E2-436F-B2E6-F6D9AAAC9361/Documents/videorecords/196F9A75-28C4-4B65-A06B-6111AEF85F01.mov
现在我想使用这样的文件位置来创建带有第一帧的缩略图,并使用以下代码将其连接到我的 ImageView :
func createVideoStills() {
for video in directoryContents {
print("\(video)")
do {
let asset = AVURLAsset(URL: NSURL(fileURLWithPath: "\(video)"), options: nil)
let imgGenerator = AVAssetImageGenerator(asset: asset)
imgGenerator.appliesPreferredTrackTransform = true
let cgImage = try imgGenerator.copyCGImageAtTime(CMTimeMake(0, 1), actualTime: nil)
let uiImage = UIImage(CGImage: cgImage)
videoCell.imageView = UIImageView(image: uiImage)
//let imageView = UIImageView(image: uiImage)
} catch let error as NSError {
print("Error generating thumbnail: \(error)")
}
}
}
第一个打印给了我一条如上所述的路径。但 AVURLAsset 不喜欢这个路径,因为它会抛出以下错误:
Error generating thumbnail: Error Domain=NSURLErrorDomain Code=-1100 "The requested URL was not found on this server." UserInfo={NSLocalizedDescription=The requested URL was not found on this server., NSUnderlyingError=0x14ee29170 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
这很奇怪,因为它就在那里。有关如何修复/解决此问题的任何解决方案吗?
亲切的问候,
沃特
最佳答案
print("\(video)")
的输出不是文件路径,而是文件URL 的字符串表示形式。您需要使用 init(string:)
而不是 NSURL
的 init(fileURLWithPath:)
。
看看你会得到什么:
let asset = AVURLAsset(URL: NSURL(string: video), options: nil)
(不必要的字符串插值会产生一些意外的结果,但不会出现错误 - 例如获取“Optional(...)”,因此您应该避免。)
关于swift - 快速无法访问文档文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38245296/