每当您要编写数百行几乎相同的代码时,都可能需要使用某种循环:
for (int currentFrame = 0; currentFrame < durationSeconds; ++currentFrame) {
CMTime currentTime = CMTimeMakeWithSeconds(i, 600);
// the rest of the code you need to create the image or whatever
}
该片段将每秒抓取一帧。如果你想每秒抓取 30 帧,它看起来更像这样:
const CGFloat framesPerSecond = 30.0;
for (int currentFrame = 0; currentFrame < (durationSeconds * framesPerSecond); ++currentFrame) {
CMTime currentTime = CMTimeMakeWithSeconds(currentFrame/framesPerSecond, 600);
// again, the code you need to create the image from this time
}
只需将值设置为framesPerSecond您想要捕获的每秒帧数即可。
作为免责声明,我对这些东西并不完全熟悉,因此 a<=可能适合此处的条件语句。
附录:我发布的代码只会获取要获取图像的时间戳。其余代码应如下所示:
AVAsset *myAsset = // your asset here
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:myAsset];
NSError *error;
CMTime actualTime;
CGImageRef currentImage = [imageGenerator copyCGImageAtTime:currentTime
actualTime:&actualTime
error:&error];
if (!error) {
[someMutableArray addObject:[[UIImage alloc] initWithCGImage:currentImage]];
}