使用iOS14.5、Swift5.4和Xcode12.5,我尝试在Swift中创建视频的缩略图。对于来自互联网的视频,这一切都可以正常工作。然而,在我的情况下,视频来自应用程序的 Library 文件夹,是之前保存的。由于某种未知的原因,似乎无法创建缩略图。
以下是我的步骤:
1. 首先,将视频从 Bundle 复制到 iPhone 沙盒的 Library 文件夹中(视频URL是本地 Library 文件夹中的某个位置)。
以下是我的步骤:
1. 首先,将视频从 Bundle 复制到 iPhone 沙盒的 Library 文件夹中(视频URL是本地 Library 文件夹中的某个位置)。
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
}
}