获取AVCaptureMovieFileOutput录制的视频的第一祯作为封面图

完整项目下载链接:https://github.com/SSYSSK/camera2

@property (strong, nonatomic) AVCaptureMovieFileOutput *movieOutput;

[self.movieOutput startRecordingToOutputFileURL: self.outputURL  recordingDelegate:self];  

这句代码时开始录制,然后就会执行代理回调

 

#pragma AVCaptureFileOutputRecordingDelegate - 视频输出代理

//录制完成
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput
didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
      fromConnections:(NSArray *)connections
                error:(NSError *)error {
     NSLog(@"录制完成");
    UISaveVideoAtPathToSavedPhotosAlbum([outputFileURL path], nil, nil, nil);
    
    //在videoQueue 上,
    dispatch_async(self.videoQueue, ^{
        
        //建立新的AVAsset & AVAssetImageGenerator
        AVAsset *asset = [AVAsset assetWithURL:outputFileURL];
        
        AVAssetImageGenerator *imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
        
        //设置maximumSize 宽为100,高为0 根据视频的宽高比来计算图片的高度
        imageGenerator.maximumSize = CGSizeMake(100.0f, 0.0f);
        
        //捕捉视频缩略图会考虑视频的变化(如视频的方向变化),如果不设置,缩略图的方向可能出错
        imageGenerator.appliesPreferredTrackTransform = YES;
        
        //获取CGImageRef图片 注意需要自己管理它的创建和释放
        CGImageRef imageRef = [imageGenerator copyCGImageAtTime:kCMTimeZero actualTime:NULL error:nil];
        
        //将图片转化为UIImage
        UIImage *image = [UIImage imageWithCGImage:imageRef];
        
        //释放CGImageRef imageRef 防止内存泄漏
        CGImageRelease(imageRef);
        
        //回到主线程
        dispatch_async(dispatch_get_main_queue(), ^{
            
            //发送请求
            NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
            [nc postNotificationName:@"showVideoImage" object:image];
            
        });
        
    });
}

 

 

这个是获取图片的代理回调(录制视频的可以忽略这里,纯粹记录一下而已)

#pragma mark AVCapturePhotoCaptureDelegate
- (void)captureOutput:(AVCapturePhotoOutput *)output didFinishProcessingPhoto:(AVCapturePhoto *)photo error:(NSError *)error {
    if (error) {
        NSLog(@"获取图片错误 --- %@",error.localizedDescription);
    }
    if (photo) {
        if (@available(iOS 11.0, *)) {
            
            CGImageRef cgImage = [photo CGImageRepresentation];
           
            UIImage * image = [UIImage imageWithCGImage:cgImage];
            NSLog(@"获取图片成功 --- %@",image);
            
            //前置摄像头拍照会旋转180解决办法
            if (self.activeVideoInput.device.position == AVCaptureDevicePositionFront) {
                UIImageOrientation imgOrientation = UIImageOrientationLeftMirrored;
                image = [[UIImage alloc]initWithCGImage:cgImage scale:1.0f orientation:imgOrientation];
            }else {
                UIImageOrientation imgOrientation = UIImageOrientationRight;
                image = [[UIImage alloc]initWithCGImage:cgImage scale:1.0f orientation:imgOrientation];
            }
            //发送请求显示缩略图
            NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
            [nc postNotificationName:@"showVideoImage" object:image];
            
            UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
        } else {
            NSLog(@"不是走这个代理方法");
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值