码迷,mamicode.com
首页 > 其他好文 > 详细

获取视频缩略图之AVAssetImageGenerator

时间:2017-12-15 14:07:43      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:track   queue   nbsp   val   nsf   nslog   bsp   获取   one   

将之前的获取视频帧的方法回顾总结一下:

方式一:获取相关帧集

 AVURLAsset *urlSet = [[AVURLAsset alloc]initWithURL:url options:nil];

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

    generator.appliesPreferredTrackTransform = YES;

    NSLog(@"generator = %@",generator);

    CMTime time = CMTimeMake(1, 30);

    AVAssetImageGeneratorCompletionHandler handler = ^(CMTime requestedTime, CGImageRef image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error){

        UIImage *thumbImg = [UIImage imageWithCGImage:image];

        if (result == AVAssetImageGeneratorSucceeded) {//成功即显示

            dispatch_async(dispatch_get_main_queue(), ^{

                cell.imageView.image = thumbImg;

                cell.tags = 1;

            });

        }

    };

    generator.maximumSize = CGSizeMake(200, 200);

    [generator generateCGImagesAsynchronouslyForTimes:

     [NSArray arrayWithObject:[NSValue valueWithCMTime:time]] completionHandler:handler];

 

方式二:得到某时刻帧特征图片       容易获取失败

    AVAsset *urlSet = [AVAsset assetWithURL:url];

    AVAssetImageGenerator *gener = [[AVAssetImageGenerator alloc]initWithAsset:urlSet];

    CMTime actualTime;

    CGImageRef imageRef = [gener copyCGImageAtTime:CMTimeMakeWithSeconds(1, 10) actualTime:&actualTime error:nil];

    CMTimeShow(actualTime);

/////这里的nserror需判断是否为nil,不为nil返回

        NSString *key = [_cameraFilesArr[indexPath.row] componentsSeparatedByString:@"."][0];

        dispatch_async(dispatch_get_main_queue(), ^{

            UIImage *thumbImg = [UIImage imageWithCGImage:imageRef];

            cell.imageView.image = thumbImg;

            [_imgCache setObject:thumbImg forKey:key];  // 缓存

        });

获取视频缩略图之AVAssetImageGenerator

标签:track   queue   nbsp   val   nsf   nslog   bsp   获取   one   

原文地址:http://www.cnblogs.com/chims-liu-touch/p/8042669.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!