每次我尝试从我的视频资产生成静止帧时,它都会在0.000 ..秒的时间生成。我可以从我的日志消息中看到这一点。好消息是我可以在0.000 ..的时间获取图像,以显示在名为“ myImageView”的UIImageView中。我以为问题是未设置AVURLAssetPreferPreciseDurationAndTimingKey,但是即使在我弄清楚该怎么做后,它仍然无法运行。

这是我所拥有的..

时间,实际时间和生成在标头中声明

NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/videoTest4.m4v"]];
//UISaveVideoAtPathToSavedPhotosAlbum(path, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
NSURL *url = [NSURL fileURLWithPath:path];

NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:options];

Float64 durationSeconds = CMTimeGetSeconds([asset duration]);

generate = [[AVAssetImageGenerator alloc] initWithAsset:asset];
NSError *err = nil;

time = CMTimeMake(600,600.0);
CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:&actualTime error:&err];
UIImage *currentImg = [[UIImage alloc] initWithCGImage:imgRef];
myImageView.image = currentImg;

NSLog(@"The total Video Duration is %f",durationSeconds);
NSLog(@"The time I want my image is %f",CMTimeGetSeconds(time));
NSLog(@"The actual time my image was take was %f",CMTimeGetSeconds(actualTime));


而我的控制台读取..

2011-04-28 18:49:59.062 videoTest [26553:207]总视频时长为1.880000

2011-04-28 18:49:59.064 videoTest [26553:207]我想要我的图像的时间是1.000000

2011-04-28 18:49:59.064 videoTest [26553:207]拍摄我的图像的实际时间是0.000000

....................................

提前谢谢你们.. :)

最佳答案

要解决此问题,您只需将AVAssetImageGenerator的requestedTimeToleranceBefore和requestedTimeToleranceAfter设置为kCMTimeZero。

AVAssetImageGenerator Class Reference

07-26 09:37
查看更多