Skip to content

Instantly share code, notes, and snippets.

@jayeshk
Last active January 10, 2017 10:18
Show Gist options
  • Save jayeshk/bc539458f886d291b27ea14e1c3ee5fd to your computer and use it in GitHub Desktop.
Save jayeshk/bc539458f886d291b27ea14e1c3ee5fd to your computer and use it in GitHub Desktop.
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);
}
}
//=====
-(void) getAllImagesFromVideo
{
imagesArray = [[NSMutableArray alloc] initWithCapacity:375];
times = [[NSMutableArray alloc] initWithCapacity:375];
for (Float64 i = 0; i < 15; i += 0.033) // For 25 fps in 15 sec of Video
{
[times addObject:[NSValue valueWithCMTime:CMTimeMakeWithSeconds(i, 60)]];
}
[imageGenerator generateCGImagesAsynchronouslyForTimes:times completionHandler:^(CMTime requestedTime, CGImageRef image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error) {
if (result == AVAssetImageGeneratorSucceeded)
{
[imagesArray addObject:[UIImage imageWithCGImage:image]];
CGImageRelease(image);
}
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment