Swift 3: 视频缩略图
Swift 3: thumbnail from video
我需要根据 Swift 中设备本地存储的视频创建缩略图 3. 我有以下代码:
func fetchFirstFrameOf(videoPath urlString: String) -> UIImage? {
let filePath = URL(fileURLWithPath: urlString).absoluteURL
let asset = AVURLAsset(url: filePath, options: nil)
let imgGenerator = AVAssetImageGenerator(asset: asset)
imgGenerator.appliesPreferredTrackTransform = false
do {
let cgImage = try imgGenerator.copyCGImage(at: CMTimeMake(0, 1), actualTime: nil)
let thumbnail = UIImage(cgImage: cgImage)
return thumbnail
} catch let error {
print("*** Error generating thumbnail: \(error)")
return nil
}
}
现在,当我拍摄并保存视频时,此功能有效。但是当我稍后重新启动应用程序时,没有显示缩略图并抛出以下错误:
*** Error generating thumbnail: Error Domain=NSURLErrorDomain Code=-1100 "The given URL was not found on this server" UserInfo={NSLocalizedDescription=The given URL was not found on this server, NSUnderlyingError=0x170255990 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
我尝试像这样初始化文件路径:URL(string: filepath),但也不成功。
拍完视频就可以了。直到您重新打开应用程序然后再次失败...
所以我认为问题是我试图访问一个临时地址(url 中的 private 和 tmp 应该会提示我:s)
已完成创建视频并将其移至文档目录。
我需要根据 Swift 中设备本地存储的视频创建缩略图 3. 我有以下代码:
func fetchFirstFrameOf(videoPath urlString: String) -> UIImage? {
let filePath = URL(fileURLWithPath: urlString).absoluteURL
let asset = AVURLAsset(url: filePath, options: nil)
let imgGenerator = AVAssetImageGenerator(asset: asset)
imgGenerator.appliesPreferredTrackTransform = false
do {
let cgImage = try imgGenerator.copyCGImage(at: CMTimeMake(0, 1), actualTime: nil)
let thumbnail = UIImage(cgImage: cgImage)
return thumbnail
} catch let error {
print("*** Error generating thumbnail: \(error)")
return nil
}
}
现在,当我拍摄并保存视频时,此功能有效。但是当我稍后重新启动应用程序时,没有显示缩略图并抛出以下错误:
*** Error generating thumbnail: Error Domain=NSURLErrorDomain Code=-1100 "The given URL was not found on this server" UserInfo={NSLocalizedDescription=The given URL was not found on this server, NSUnderlyingError=0x170255990 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
我尝试像这样初始化文件路径:URL(string: filepath),但也不成功。
拍完视频就可以了。直到您重新打开应用程序然后再次失败...
所以我认为问题是我试图访问一个临时地址(url 中的 private 和 tmp 应该会提示我:s)
已完成创建视频并将其移至文档目录。