1

I am creating an iPhone app in which user will be able to capture the video and when he will done with that, the first frame of that video should be shown as a thumbnail image. How can I show that image, I mean how can I extract the first frame of video? Thanks-

Developer
  • 6,375
  • 12
  • 58
  • 92

1 Answers1

5

There are two options:-

1) Using the MPMoviePlayerController.

MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc]
                                       initWithContentURL:videoURL];
moviePlayer.shouldAutoplay = NO;
UIImage *thumbnail = [moviePlayer thumbnailImageAtTime:time
                     timeOption:MPMovieTimeOptionNearestKeyFrame];

2) Using AVURLAsset

AVURLAsset *asset=[[AVURLAsset alloc] initWithURL:self.url options:nil];
AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generator.appliesPreferredTrackTransform=TRUE;
[asset release];
CMTime thumbTime = CMTimeMakeWithSeconds(0,30);

AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef im, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){
    if (result != AVAssetImageGeneratorSucceeded) {
        NSLog(@"couldn't generate thumbnail, error:%@", error);
    }
    [button setImage:[UIImage imageWithCGImage:im] forState:UIControlStateNormal];
    thumbImg=[[UIImage imageWithCGImage:im] retain];
    [generator release];
};

CGSize maxSize = CGSizeMake(320, 180);
generator.maximumSize = maxSize;
[generator generateCGImagesAsynchronouslyForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:thumbTime]] completionHandler:handler];
Anil Kothari
  • 7,653
  • 4
  • 20
  • 25
  • Thanks Anil, Let me try these methods and I will get back to you with response. – Developer Mar 09 '12 at 06:32
  • The first option using MPMoviePlayerController worked great for me. – Brian Robbins Nov 15 '12 at 22:35
  • `[thumbnailImageAtItem:timeOption:]` was deprecated with the release of iOS7. `[requestThumbnailImagesAtTimes:timeOption]` is the new API call expecting an NSArray of NSNumber objects containing the times at which to capture the thumbnail images. – MrBr Mar 18 '14 at 07:08