iOS Swift:视频缩略图错误 [英] iOS Swift: video thumbnail error
本文介绍了iOS Swift:视频缩略图错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下代码创建视频缩略图,并且在大多数情况下都可以使用.但是有时会引发错误,并且不会创建缩略图.我不知道为什么有时不起作用.
I'm creating video thumbnails with the following code and it works in most cases. However sometimes it throws an error and the thumbnail is not created. I can't figure out why it does not work sometimes.
这是我创建视频缩略图的方法:
Here is how I create video thumbnail:
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)")
}
在大多数情况下它可以工作,当它不工作时会引发此错误:
It works in most cases, when it does not work it throws this error:
Error generating thumbnail: Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x17658520 {Error Domain=NSOSStatusErrorDomain Code=-12792 "(null)"}, NSLocalizedFailureReason=An unknown error occurred (-12792)}
我不知道为什么它会引发此错误,并且即使其他人面临相同的问题,也没有在堆栈溢出时找到任何解决方案.
I have no clue why it throws this error and did not find any solution on stack overflow even though other people faced same problem.
推荐答案
尝试一下.
func generateThumbnail(path: URL) -> UIImage? {
do {
let asset = AVURLAsset(url: path, options: nil)
let imgGenerator = AVAssetImageGenerator(asset: asset)
imgGenerator.appliesPreferredTrackTransform = true
let cgImage = try imgGenerator.copyCGImage(at: CMTimeMake(0, 1), actualTime: nil)
let thumbnail = UIImage(cgImage: cgImage)
return thumbnail
} catch let error {
print("*** Error generating thumbnail: \(error.localizedDescription)")
return nil
}
}
这篇关于iOS Swift:视频缩略图错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文