iOS Swift:错误域= NSOSStatusErrorDomain代码= -12792?

时间:2016-05-23 09:38:36

标签: ios error-handling avfoundation nserror video-thumbnails

我尝试使用以下代码获取视频缩略图:

let asset = AVAsset(URL: url)
    let imageGenerator = AVAssetImageGenerator(asset: asset)
    imageGenerator.appliesPreferredTrackTransform = true

    do {
        let cgImage = try imgGenerator.copyCGImageAtTime(CMTimeMake(1, 30), actualTime: nil)
        let uiImage = UIImage(CGImage: cgImage)
        imageview.image = uiImage
    }
    catch let error as NSError
    {
        print("Image generation failed with error \(error)")
    }

有时它会起作用,有时它不会显示以下错误:

  

错误域= AVFoundationErrorDomain代码= -11800"操作可以   没完成" UserInfo = {NSLocalizedDescription =操作可以   未完成,NSUnderlyingError = 0x14eab520 {错误   Domain = NSOSStatusErrorDomain Code = -12792"(null)"},   NSLocalizedFailureReason =发生未知错误(-12792)}

我试图找出Domain=NSOSStatusErrorDomain Code=-12792是什么,但我不明白如何获得有关此错误代码的更多详细信息。如何将此错误代码转换为字符串以获取有关此错误含义的相关信息?

1 个答案:

答案 0 :(得分:0)

我可以通过以下方法解决此问题。

Swift 4.1

func createThumbnailForVideo(atURL videoURL: URL , completion : @escaping (UIImage?)->Void) {
    let asset = AVAsset(url: videoURL)
    let assetImgGenerate = AVAssetImageGenerator(asset: asset)
    assetImgGenerate.appliesPreferredTrackTransform = true
    let time = CMTimeMakeWithSeconds(1, preferredTimescale: 60)
    let times = [NSValue(time: time)]
    assetImgGenerate.generateCGImagesAsynchronously(forTimes: times, completionHandler: {  _, image, _, _, _ in
        if let image = image {
            let uiImage = UIImage(cgImage: image)
            completion(uiImage)
        } else {
            completion(nil)
        }
    })
}

希望这会有所帮助。