Developer
Developer

Reputation: 6465

How to show the first frame of video as a Thumbnail image in iPhone App?

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-

Upvotes: 1

Views: 3521

Answers (1)

Anil Kothari
Anil Kothari

Reputation: 7733

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];

Upvotes: 5

Related Questions