swift - 快速无法访问文档文件路径

标签 swift documents video-thumbnails

我目前正在开发一个小型 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:) 而不是 NSURLinit(fileURLWithPath:)

看看你会得到什么:

            let asset = AVURLAsset(URL: NSURL(string: video), options: nil)

(不必要的字符串插值会产生一些意外的结果,但不会出现错误 - 例如获取“Optional(...)”,因此您应该避免。)

关于swift - 快速无法访问文档文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38245296/

相关文章:

arrays - 有没有更好的方法在 Swift textView 中对齐这列数字?

swift - 如何创建具有不同数据类型的数据结构?

php - 如何将文件更改为仅当用户查看时才读取?

ffmpeg - 如何使用正确的时间码生成带有视频缩略图的图 block

performance - 使用 FFMPEG 提取缩略图在大型视频文件上 super 慢?

ios - 添加 segue 时标签栏丢失

ios - 关闭 UITextField 的键盘

xslt - xsl fo 页面大小/方向

iphone - 我的应用程序中的 iTunes 文档目录

javascript - 缩略图视频react-native-image-picker