我正在尝试从视频中获取所有帧,但它总是在
处失败cgImage = try assetImgGenerate.copyCGImageAtTime(time, actualTime: nil)
我当前的代码看起来像这样
if let path = NSBundle.mainBundle().resourcePath {
let fullPath = path + "/mov/watch_phone.mov"
let url = NSURL(fileURLWithPath: fullPath)
mutableVideoURL = url
getFrame()
print(videoFrames.count)
}
func getFrame() {
let asset:AVAsset = AVAsset(URL:self.mutableVideoURL as NSURL)
let assetImgGenerate:AVAssetImageGenerator = AVAssetImageGenerator(asset:asset)
assetImgGenerate.appliesPreferredTrackTransform = true
let duration:Float64 = CMTimeGetSeconds(asset.duration)
let durationInt:Int = Int(duration)
for index:Int in 0 ..< durationInt
{
generateFrames(assetImgGenerate, fromTime: Float64(index))
}
}
func generateFrames(assetImgGenerate:AVAssetImageGenerator, fromTime:Float64)
{
let time:CMTime = CMTimeMakeWithSeconds(fromTime, 600)
let cgImage:CGImage?
do
{
cgImage = try assetImgGenerate.copyCGImageAtTime(time, actualTime: nil)
let frameImg:UIImage = UIImage(CGImage:cgImage!)
videoFrames.append(frameImg)
}
catch let error as NSError {
print("Found error in saving entity changes \(error)")
}
copyCGImageAtTime
总是返回nil。
该视频已添加到我的捆绑包中,并且还显示在Build Phases中的捆绑包资源中。
这是catch块中收到的错误
Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSUnderlyingError=0x1700563e0 {Error Domain=NSOSStatusErrorDomain Code=-12430 "(null)"}, NSLocalizedFailureReason=An unknown error occurred (-12430), NSLocalizedDescription=The operation could not be completed}
请帮助我理解我在这里做错了什么
答案 0 :(得分:0)
这是一个有效的代码。试试这个并确保你有非零资产,正确的时间。
- (UIImage *)getUIImageOfVideoAsset:(AVAsset *)asset atTime:(CMTime)time withSize:(CGSize)size {
NSError *error;
AVAssetImageGenerator *baseImageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
baseImageGenerator.appliesPreferredTrackTransform = YES;
baseImageGenerator.maximumSize = size;
CGImageRef cgImage = [baseImageGenerator copyCGImageAtTime:time actualTime:NULL error:&error];
UIImage *image = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
return image;
}