0

I can't seem to reliably fetch a thumbnail image from a video URL in Swift 5. Sometimes it works, but fails more often than not. If you can help, please do. Thanks in advance. (I've tried this both on simulator & a real device)

Edit: I checked the whole error, as opposed to merely the localizedDescription, and I got:

Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSLocalizedFailureReason=An unknown error occurred (-12792), NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x281998a80 {Error Domain=NSOSStatusErrorDomain Code=-12792 "(null)"}}

Note- my app is allowing arbitrary loads in info.plist

static func getThumbnailImageFromVideoUrl(url: URL, completion: @escaping ((_ image: UIImage?)->Void)) {
    DispatchQueue.global().async {
        let asset = AVAsset(url: url)
        let avAssetImageGenerator = AVAssetImageGenerator(asset: asset)
        avAssetImageGenerator.appliesPreferredTrackTransform = true
        let thumbnailTime = CMTimeMake(value: 2, timescale: 1)
        do {
            let cgThumbImage = try avAssetImageGenerator.copyCGImage(at: thumbnailTime, actualTime: nil)
            let thumbImage = UIImage(cgImage: cgThumbImage)
            DispatchQueue.main.async {
                completion(thumbImage)
            }
        } catch {
            print("Thumbnail not fetched, with error: \"\(error.localizedDescription)\"")
            DispatchQueue.main.async {
                completion(nil)
            }
        }
    }
}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • `print("Thumbnail not fetched, with error: \"\(error.localizedDescription)\"")` => `print("Thumbnail not fetched, with error: \"\(error)\"")` it might give more infos. – Larme Mar 23 '21 at 09:16
  • 2
    https://stackoverflow.com/questions/37387551/ios-swift-error-domain-nsosstatuserrordomain-code-12792 ? – Larme Mar 23 '21 at 09:34
  • Thanks so much, that fixed my problem. This was my first post on stack so apologies if I did anything wrong, besides not searching for that other question. – David Reese Mar 23 '21 at 09:42

0 Answers0