生成视频截图 计算时间差

2016-12-10  本文已影响47人  Jackson_Z
#pragma mark - WechatShortVideoDelegate代理方法

-(void)finishWechatShortVideoCapture:(NSURL *)filePath{
    //已经取得视频的url路径 然后只需要获取获取它的封面图既可
    //  创建视频资源
    AVAsset *asset = [AVAsset assetWithURL:filePath];
    
    //  使用视频 资源创建一个图片生成器
    AVAssetImageGenerator *imgeGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
    //  视频专用的时间
    
    //  preferredTimeScale: 每秒多少帧
    CMTime time = CMTimeMakeWithSeconds(0.0,asset.duration.timescale);
    //  时间
    NSValue *timeValue = [NSValue valueWithCMTime:time];
    //  生成该时间对应图片
    NSTimeInterval statTime = CFAbsoluteTimeGetCurrent();
    [imgeGenerator generateCGImagesAsynchronouslyForTimes:@[timeValue] completionHandler:^(CMTime requestedTime, CGImageRef  _Nullable image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError * _Nullable error) {
        
        HPLog(@"%@", [NSThread currentThread]);
        
        // 在bock中转换为UIImage,然后异步赋值
        UIImage *uiImage = [UIImage imageWithCGImage:image];
        dispatch_async(dispatch_get_main_queue(), ^{
            
            NSTimeInterval endTime = CFAbsoluteTimeGetCurrent();

#pragma mark - 一般认为耗时 <2 是连续点击 不上传服务器  点击停止在上传

            HPLog(@"耗时:%lf",endTime - statTime);
            HPLog(@"封面图%@",uiImage);
            HPLog(@"视频路径:%@",filePath.path);
            
            [self addVideoConentWith:uiImage OrPostImageUrlString:nil videoUrlString:filePath];
            
        });
    }];
}
上一篇下一篇

猜你喜欢

热点阅读