如何解决如果视频来自本地库文件夹,则视频缩略图生成失败
使用 iOS14.5、Swift5.4、Xcode12.5,
我尝试在 Swift 中创建视频的缩略图。
这一切都适用于源自互联网的视频。
但是,在我的情况下,视频源自应用程序的库文件夹,其中预先保留了该文件夹。由于某种未知原因,这似乎无法创建缩略图。
这是我的步骤:
guard fileManager.fileExists(atPath: videoURL.path) else {
guard let video = NSDataAsset(name: videoName) else { return nil }
fileManager.createFile(atPath: videoURL.path,contents: video.data,attributes: nil)
}
VideoThumb image generation Failed with error Error Domain=AVFoundationErrorDomain
Code=-11850 "Operation Stopped" UserInfo={NSLocalizedFailureReason=The server is not
correctly configured.,NSLocalizedDescription=Operation Stopped,NSUnderlyingError=0x2817da100 {Error Domain=NSOsstatusErrorDomain Code=-12939 "(null)"}}
private func generateThumbImage(for videoURL: URL) -> UIImage? {
let asset = AVAsset(url: videoURL)
let assetImgGenerate : AVAssetimageGenerator = AVAssetimageGenerator(asset: asset)
assetImgGenerate.appliesPreferredTrackTransform = true
assetImgGenerate.apertureMode = .encodedPixels
let time = CMTimeMake(value: 50,timescale: 60)
do {
let imageRef = try assetImgGenerate.copyCGImage(at: time,actualTime: nil)
return UIImage(cgImage: imageRef)
}
catch {
!!!!!!!!!!!!!!!!!!!!!!!!!!! Always hit this catch here - WHY ???????????????????????
print("VideoThumb image generation Failed with error \(error)")
return nil
}
}
解决方法
我终于找到了解决办法:
您需要在本地网址前添加 file:
这里是调用缩略图生成的工作代码:
if let vidThumb = generateThumbnail(videoURL: URL(string: "file:" + url.absoluteString)!) { ... }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。