获取Video IOS 6的所有帧 [英] Get all frames of Video IOS 6
问题描述
我想将iOS6中视频的所有帧转换为 NSArray
。
我使用此代码:
I would like to get all frames of a Video in iOS6 into NSArray
.
I use this code:
-(void) getAllImagesFromVideo
{
imagesArray = [[NSMutableArray alloc] init];
times = [[NSMutableArray alloc] init];
for (Float64 i = 0; i < 15; i += 0.033) // For 25 fps in 15 sec of Video
{
CMTime time = CMTimeMakeWithSeconds(i, 60);
[times addObject:[NSValue valueWithCMTime:time]];
}
[imageGenerator generateCGImagesAsynchronouslyForTimes:times completionHandler:^(CMTime requestedTime, CGImageRef image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error) {
if (result == AVAssetImageGeneratorSucceeded)
{
UIImage *generatedImage = [[UIImage alloc] initWithCGImage:image];
[imagesArray addObject:generatedImage];
}
}];
}
在iPad模拟器上,延迟时间为90~100秒,在iPad设备上,已收到内存警告并最终崩溃。
On iPad simulator the delay is 90~100 seconds, on iPad device, recieved Memory Warnings and finally crash.
任何想法,解决方案?使用另一个更低级的框架/库? C ++?
对我来说非常重要!帮我! :)
Any idea, solution? Using another more low-level Framework/Library? C++? Is very important for me! Help me! :)
谢谢!!!
推荐答案
您需要:
- 使用CGImageRelease作为@zimmryan提到
- 在循环中使用@autoreleasepool块
- 不将图像存储在内存中,将它们存储在文档目录中
这是怎么回事我这样做:(注意我使用同步版本来提取图像,如果选择异步版本则无关紧要)
This is how I do it: (notice I use the sync version for extracting images, it shouldn't matter if you choose the async version)
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.requestedTimeToleranceAfter = kCMTimeZero;
generator.requestedTimeToleranceBefore = kCMTimeZero;
for (Float64 i = 0; i < CMTimeGetSeconds(asset.duration) * FPS ; i++){
@autoreleasepool {
CMTime time = CMTimeMake(i, FPS);
NSError *err;
CMTime actualTime;
CGImageRef image = [generator copyCGImageAtTime:time actualTime:&actualTime error:&err];
UIImage *generatedImage = [[UIImage alloc] initWithCGImage:image];
[self saveImage: generatedImage atTime:actualTime]; // Saves the image on document directory and not memory
CGImageRelease(image);
}
}
编辑:
在创建大量临时对象时,应始终考虑使用@autoreleasepool(参见 https://developer.apple.com/library/mac/documentation/cocoa/conceptual/memorymgmt/articles/mmAutoreleasePools.html )
you should always consider using @autoreleasepool when creating alot of temporary objects (see https://developer.apple.com/library/mac/documentation/cocoa/conceptual/memorymgmt/articles/mmAutoreleasePools.html)
这篇关于获取Video IOS 6的所有帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!