从视频中截图有两种方法

来源:互联网 发布:mysql 如何启动 编辑:程序博客网 时间:2024/05/13 15:54

原文出自:http://blog.sina.com.cn/s/blog_8a6c97b501017h5c.html


从视频中截图有两种方法,一种是用MPMoviePlayerController,另一种用AVAssetImageGenerator。请直接看代码。

1.用MPMoviePlayerController

+(UIImage *)thumbnailFromVideoAtPath:(NSString*)videoFilePath
{
  NSURL *url = [NSURLfileURLWithPath:videoFilePath];
  MPMoviePlayerController *player = [[MPMoviePlayerControlleralloc] initWithContentURL:url];
  UIImage *image= [playerthumbnailImageAtTime:2.0 timeOption:MPMovieTimeOptionNearestKeyFrame];
  [playerstop];
  [playerrelease];
 
  return image;
}

2.用AVAssetImageGenerator

+(UIImage *)thumbnailFromVideoAtPath:(NSString*)videoFilePath
{
    NSURL *url = [NSURL fileURLWithPath:videoFilePath];
    AVURLAsset *asset = [[[AVURLAsset alloc]initWithURL:url options:nil] autorelease];
    AVAssetImageGenerator*generator =[[[AVAssetImageGenerator alloc]initWithAsset:asset] autorelease];
   
    NSError *error = nil;
    CMTime time =CMTimeMakeWithSeconds(2.0,60);
    CGImageRef imgRef= [generatorcopyCGImageAtTime:time actualTime:NULL error:&error];
    if(error.description != nil) NSLog(@"Error:(thumbnailFromVideoAtPath:)%@",error.description);
    UIImage *image = [[[UIImage alloc]initWithCGImage:imgRef] autorelease];
    CFRelease(imgRef);
   
    return image;
}

原创粉丝点击