Using iOS14.5, Swift5.4, Xcode12.5,
I try to create a thumbnail image of a video in Swift.
It all works for a video that originates from the Internet.
However, in my case, the video originates from the Library Folder of the App where it was persisted beforehand. And that seems to fail the thumbnail creation for some unknown reason.
Here are my steps:
- I first copy the video from the Bundle to the Library Folder of the iPhone Sandbox by doing the following (videoURL being some location in the local Library folder)
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)
}
- Then I try to generate a thumbnail image by the below code.
But this code always fails with the following error:
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)"}}
Here the code for the thumbnail generation:
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
}
}
I finally found a solution:
You need to add
file:
prior to your local URLHere the working code for calling the thumbnail-generation:
if let vidThumb = generateThumbnail(videoURL: URL(string: "file:" + url.absoluteString)!) { ... }