2017-01-30 57 views
0

我使用下面的代碼使用AVAsset類生成縮略圖的失敗,錯誤代碼AVFoundationErrorDomain = -11800

func generateThumnail(url :URL, fromTime:Float64) -> UIImage? { 
    let asset :AVAsset = AVAsset(url:url) 
    let assetImgGenerate : AVAssetImageGenerator = AVAssetImageGenerator(asset: asset) 
    assetImgGenerate.appliesPreferredTrackTransform = true 
    assetImgGenerate.requestedTimeToleranceAfter = kCMTimeZero; 
    assetImgGenerate.requestedTimeToleranceBefore = kCMTimeZero; 

    let time  : CMTime = CMTimeMakeWithSeconds(1,30) 

    do { 
     let img = try assetImgGenerate.copyCGImage(at: time, actualTime: nil) 
     return UIImage(cgImage: img) 
    } catch let error as NSError { 
     print("Image generation failed with error \(error)") 
     return nil 
    } 
    return nil 
} 

如果失敗,有時與相同的視頻網址

以下錯誤往往礦石生成遠程視頻的網址

AVFoundationErrorDomain代碼= -11800

不知道什麼是錯與上面的代碼我是新來AVAsset一個ND相關框架 任何幫助表示讚賞

+0

這些碰撞是隨機 –

+0

要生成遠程資產縮略圖,在這裏看到:http://stackoverflow.com/a/29432405/22147 –

回答

0
func getVideoThumbNail(_ videoName: String) -> UIImage { 
    do { 
     let url = URL(fileURLWithPath: getDocumentsDirectory().appendingPathComponent(videoName)) 
     let asset = AVURLAsset(url: url, options: nil) 
     let imgGenerator = AVAssetImageGenerator(asset: asset) 
     imgGenerator.appliesPreferredTrackTransform = true 
     let cgImage = try imgGenerator.copyCGImage(at: CMTimeMake(2, 3), actualTime: nil) 
     return UIImage(cgImage: cgImage) 
    } catch let error as NSError { 
     print("\(ERROR_GENERATING_THUMNAIL): \(error)") 
    } 
    return ERROR_GENERATING_THUMNAIL 
} 

func getDocumentsDirectory() -> NSString { 
    let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) 
    return paths[0] as NSString 
} 
+0

fileURLwithPath應該對影片工作存儲在本地文件系統,如果我沒有錯 –

+0

是的,你說得對。 \t fileURLwithPath適用於存儲在本地文件系統中的視頻。 – Tejas

+0

我的視頻來自互聯網 –

相關問題