AVAssetImageGenerator - 在5秒内生成缩略图

时间:2016-09-13 22:05:17

标签: ios avfoundation avasset avassetimagegenerator

看起来很简单。我只想让缩略图从5秒进入视频:

AVURLAsset *asset1 = [[AVURLAsset alloc] initWithURL:[NSURL URLWithString:url] options:nil];
AVAssetImageGenerator *generate1 = [[AVAssetImageGenerator alloc] initWithAsset:asset1];
generate1.appliesPreferredTrackTransform = YES;
NSError *err = NULL;
Float64 seconds = 5;
int32_t preferredTimeScale = 30;
CMTime actualTime;
CMTime inTime = CMTimeMakeWithSeconds(seconds, preferredTimeScale);
CMTimeShow(inTime);
CGImageRef oneRef = [generate1 copyCGImageAtTime:inTime actualTime:&actualTime error:&err];

输出是这样的:

Printing description of actualTime:
(CMTime) actualTime = 83 1000ths of a second {
  value = 83
  timescale = 1000
  flags = kCMTimeFlags_Valid
  epoch = 0
}
Printing description of inTime:
(CMTime) inTime = 150 30ths of a second {
  value = 150
  timescale = 30
  flags = kCMTimeFlags_Valid
  epoch = 0
}

为什么actualTimeinTime不同?如何在视频的5秒钟内获取图像?

1 个答案:

答案 0 :(得分:0)

尝试使用requestedTimeToleranceBeforerequestedTimeToleranceAfter

generate1.requestedTimeToleranceBefore = kCMTimeZero
generate1.requestedTimeToleranceAfter = kCMTimeZero
  

var requestedTimeToleranceBefore:CMTime

     

图像所需时间之前的最长时间   可能会生成。

     

var requestedTimeToleranceAfter:CMTime

     

图像请求时间之后的最长时间   可能会生成。