从视频中获取截图

从视频中获取截图

从视频中获取截图:

NSString *movpath =[[NSBundle mainBundle] pathForResource:@”iosxcode4″ ofType:@”mov”];

mpviemController =[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:movpath]];

MPMoviePlayerController *mp=[mpviemController moviePlayer];

UIImage *thumbImage=[mp thumbnailImageAtTime:second timeOption:MPMovieTimeOptionNearestKeyFrame];

NSData *imagedata =UIImagePNGRepresentation(thumbImage);

[imagedata writeToFile:@"iosxcode4.png" atomically:YES];

第二种方式 直接为UIImage写一个分类:

+(UIImage *)getImage:(NSString *)videoURL
{
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:videoURL] options:nil];
    AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    gen.appliesPreferredTrackTransform = YES;
    CMTime time = CMTimeMakeWithSeconds(0.0, 600);
    NSError *error = nil;
    CMTime actualTime;
    CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
    UIImage *thumb = [[UIImage alloc] initWithCGImage:image];
    CGImageRelease(image);
    return thumb;
}

04-26 09:22