首页 > 代码库 > 第五十二篇、 OC获取视频的第一帧图片thumbnailImage

第五十二篇、 OC获取视频的第一帧图片thumbnailImage

获取视频的第一帧图片 
有时候我们拍摄完视频后,希望获取一张图片当作这个视频的介绍,这个图片thumbnailImage可以从视频的第一帧获取到。

我们的思路是先获取视频的URL,然后初始化一个MPMoviePlayerController做工具,利用MPMoviePlayerController来拿图片,看具体代码如下:

- (void)imagePickerController:(UIImagePickerController *)picker   didFinishPickingMediaWithInfo:(NSDictionary *)info{NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];if (CFStringCompare ((__bridge CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {    NSURL *videoUrl=(NSURL*)[info objectForKey:UIImagePickerControllerMediaURL];    NSString *moviePath = [videoUrl path];    这里利用MPMoviePlayerController来获取            MPMoviePlayerController *player = [[MPMoviePlayerController alloc]initWithContentURL:videoUrl] ;            UIImage  *thumbnail = [player thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];            imageV.image = thumbnail;            player = nil;//释放player    NSString *videoCacheDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/UploadPhoto/"];    if (![[NSFileManager defaultManager] fileExistsAtPath:videoCacheDir]) {        [[NSFileManager defaultManager] createDirectoryAtPath:videoCacheDir withIntermediateDirectories:YES attributes:nil error:nil];    }}[self dismissViewControllerAnimated:YES completion:nil];}

 

第五十二篇、 OC获取视频的第一帧图片thumbnailImage