AVAsset *asset = [AVAsset assetWithURL:vIDeoUrl];CMTime vIDLength = asset.duration;float seconds = CMTimeGetSeconds(vIDLength);int frameCount = 0;for (float i = 0; i < seconds;) { AVAssetimageGenerator *imageGenerator = [[AVAssetimageGenerator alloc]initWithAsset:asset]; CMTime time = CMTimeMake(i,10); CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NulL error:NulL]; UIImage *thumbnail = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef);Nsstring* filename = [Nsstring stringWithFormat:@"documents/frame_%d.png",frameCount];Nsstring* pngPath = [NSHomeDirectory() stringByAppendingPathComponent:filename];[UIImagePNGRepresentation(thumbnail) writetofile: pngPath atomically: YES];frameCount++;i = i + 0.1;}
但是,不是在视频的当前时间i获取帧,而是获得初始帧?
如何每秒10次获取视频帧?
谢谢您的帮助 :)
解决方法 您正在获取初始帧,因为您尝试使用浮点值创建CMTime:CMTime time = CMTimeMake(i,10);
由于CMTimeMake函数将int64_t值作为第一个参数,因此您的float值将四舍五入为int,并且您将得到不正确的结果.
让我们稍微改变你的代码:
1)首先,您需要查找需要从视频中获取的总帧数.你写道,你需要每秒10帧,所以代码将是:
int requiredFramesCount = seconds * 10;
2)接下来,您需要找到一个值,该值将增加每个步骤的CMTime值:
int64_t step = vIDLength.value / requiredFramesCount;
3)最后,您需要将requestedTimetoleranceBefore和requestedTimetoleranceAfter设置为kCMTimeZero,以便在精确时间获取帧:
imageGenerator.requestedTimetoleranceAfter = kCMTimeZero;imageGenerator.requestedTimetoleranceBefore = kCMTimeZero;
以下是您的代码的外观:
CMTime vIDLength = asset.duration;float seconds = CMTimeGetSeconds(vIDLength);int requiredFramesCount = seconds * 10;int64_t step = vIDLength.value / requiredFramesCount;int value = 0;for (int i = 0; i < requiredFramesCount; i++) { AVAssetimageGenerator *imageGenerator = [[AVAssetimageGenerator alloc]initWithAsset:asset]; imageGenerator.requestedTimetoleranceAfter = kCMTimeZero; imageGenerator.requestedTimetoleranceBefore = kCMTimeZero; CMTime time = CMTimeMake(value,vIDLength.timescale); CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NulL error:NulL]; UIImage *thumbnail = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); Nsstring* filename = [Nsstring stringWithFormat:@"documents/frame_%d.png",i]; Nsstring* pngPath = [NSHomeDirectory() stringByAppendingPathComponent:filename]; [UIImagePNGRepresentation(thumbnail) writetofile: pngPath atomically: YES]; value += step;}总结
以上是内存溢出为你收集整理的iOS拍摄多个屏幕截图全部内容,希望文章能够帮你解决iOS拍摄多个屏幕截图所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)