15

I'm using UIImagePicker to allow the user to create a video and then trim it. I need to split that video into multiple frames and let the user choose one of them.

In order to show the frames I likely must convert them to UIImage. How can I do this? I must use AVFoundation but I couldn't find a tutorial on how to get & convert the frames.

Should I do the image capture with AVFoundation too? If so do I have to implementing trimming myself?

MB.
  • 4,167
  • 8
  • 52
  • 79

3 Answers3

14

I think the answer in this question is what you are looking for.

iPhone Read UIimage (frames) from video with AVFoundation.

There are 2 methods specified by the accepted answer. You can use either one according to your requirement.

Community
  • 1
  • 1
Robin
  • 10,011
  • 5
  • 49
  • 75
9

Here is code to get FPS images from video

1) Import

#import <Photos/Photos.h>

2) in viewDidLoad

    videoUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"VfE_html5" ofType:@"mp4"]];
    [self createImage:5]; // 5 is frame per second (FPS) you can change FPS as per your requirement.

3) Functions

-(void)createImage:(int)withFPS {
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoUrl options:nil];
    AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    generator.requestedTimeToleranceAfter =  kCMTimeZero;
    generator.requestedTimeToleranceBefore =  kCMTimeZero;

    for (Float64 i = 0; i < CMTimeGetSeconds(asset.duration) *  withFPS ; i++){
        @autoreleasepool {
            CMTime time = CMTimeMake(i, withFPS);
            NSError *err;
            CMTime actualTime;
            CGImageRef image = [generator copyCGImageAtTime:time actualTime:&actualTime error:&err];
            UIImage *generatedImage = [[UIImage alloc] initWithCGImage:image];
            [self savePhotoToAlbum: generatedImage]; // Saves the image on document directory and not memory
            CGImageRelease(image);
        }
    }
}

-(void)savePhotoToAlbum:(UIImage*)imageToSave {

    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        PHAssetChangeRequest *changeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:imageToSave];
    } completionHandler:^(BOOL success, NSError *error) {
        if (success) {
            NSLog(@"sucess.");
        }
        else {
            NSLog(@"fail.");
        }
    }];
}
Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81
0

You also can use the lib VideoBufferReader (see on GitHub), based on AVFoundation.

rocambille
  • 15,398
  • 12
  • 50
  • 68
CrimeZone
  • 230
  • 2
  • 13