美文网首页
视频的截图

视频的截图

作者: KAKA_move | 来源:发表于2016-12-23 11:48 被阅读0次

    #import "ViewController.h"#import@interface ViewController ()@property (weak, nonatomic) IBOutlet UIImageView *imageView;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];  }- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event

    {

    // 获取视频url

    NSURL *url = [[NSBundle mainBundle] URLForResource:@"Alizee_La_Isla_Bonita.mp4" withExtension:nil];

    // 设置截图的资源

    AVAsset *asset = [AVAsset assetWithURL:url];

    // 创建视频截图对象

    AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];

    // 开始截图

    // CMTimeMake 参数1 / 参数2 = 时间

    CMTime time = CMTimeMake(30, 2);

    NSValue *value = [NSValue valueWithCMTime:time];

    // 参数1 :截图的时间

    [generator generateCGImagesAsynchronouslyForTimes:@[value] completionHandler:^(CMTime requestedTime, CGImageRef  _Nullable image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError * _Nullable error) {

    if (error) {

    NSLog(@"%@",error);

    }

    UIImage *uiimage = [UIImage imageWithCGImage:image];

    [self performSelectorOnMainThread:@selector(updateUI:) withObject:uiimage waitUntilDone:NO];

    }];

    }

    - (void)updateUI:(UIImage *)image

    {

    self.imageView.image = image;

    }

    @end

    相关文章

      网友评论

          本文标题:视频的截图

          本文链接:https://www.haomeiwen.com/subject/ibrkvttx.html