一、使用UIImagePickerController进行拍摄设置
// 一般情况下用这个判断 该设备支不支持拍照
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[MBProgressHUD showError:@"该设备不支持拍照"];
return ;
}
//实现摄像
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
//指定使用模式
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
//设置相机支持的类型,拍照和录像
picker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString*)kUTTypeMovie,nil];
//能否编辑
picker.allowsEditing = NO;
//设置闪光灯模式
picker.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
// 视频的最大录制时长
picker.videoMaximumDuration = 30.f;
// 设置视频的质量
picker.videoQuality = UIImagePickerControllerQualityTypeMedium;
//相机的模式 拍照/摄像
picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
//协议
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
二、获取拍摄完成后的视频
//视频完成后方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
// 拿到视频地址
NSURL *sourceURL = [info objectForKey:UIImagePickerControllerMediaURL];
// 配置输出的视频文件,保存在app自己的沙盒路径里,在上传后删除掉。
NSURL *newVideoUrl;
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
//用时间给文件全名,以免重复,在测试的时候其实可以判断文件是否存在若存在,则删除,重新生成文件即可
[formater setDateFormat:@"yyyy-MM-dd-HH:mm:ss"];
newVideoUrl = [NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingFormat:@"/Documents/output-%@.mp4", [formater stringFromDate:[NSDate date]]]] ;
[picker dismissViewControllerAnimated:YES completion:nil];
// 处理视频 压缩视频
[self convertVideoQuailtyWithInputURL:sourceURL outputURL:newVideoUrl completeHandler:nil];
}
三、压缩视频
// 视频压缩转码处理
- (void)convertVideoQuailtyWithInputURL:(NSURL*)inputURL
outputURL:(NSURL*)outputURL
completeHandler:(void (^)(AVAssetExportSession*))handler {
AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:inputURL options:nil];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:avAsset presetName:AVAssetExportPresetMediumQuality];
exportSession.outputURL = outputURL;
exportSession.outputFileType = AVFileTypeMPEG4;
exportSession.shouldOptimizeForNetworkUse= YES;
[exportSession exportAsynchronouslyWithCompletionHandler:^(void) {
switch (exportSession.status) {
case AVAssetExportSessionStatusCancelled:
NSLog(@"AVAssetExportSessionStatusCancelled");
break;
case AVAssetExportSessionStatusUnknown:
NSLog(@"AVAssetExportSessionStatusUnknown");
break;
case AVAssetExportSessionStatusWaiting:
NSLog(@"AVAssetExportSessionStatusWaiting");
break;
case AVAssetExportSessionStatusExporting:
NSLog(@"AVAssetExportSessionStatusExporting");
break;
case AVAssetExportSessionStatusCompleted:
NSLog(@"AVAssetExportSessionStatusCompleted");
// 导出完成后再赋值
NSLog(@"压缩成功");
//视频路径
self.outputURL = outputURL;
//上传视频逻辑
break;
case AVAssetExportSessionStatusFailed:
NSLog(@"AVAssetExportSessionStatusFailed");
break;
}
}];
}
//获取视频缩略图
- (UIImage *)get_videoThumbImage:(NSURL *)videoURL
{
NSDictionary *opts = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:AVURLAssetPreferPreciseDurationAndTimingKey];
AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:videoURL options:opts];
AVAssetImageGenerator *generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:urlAsset];
generator.appliesPreferredTrackTransform = YES;
CMTime actualTime;
NSError *error = nil;
CGImageRef img = [generator copyCGImageAtTime:CMTimeMake(0, 600) actualTime:&actualTime error:&error];
if (error) {
return nil;
}
return [UIImage imageWithCGImage:img];
}
四、封装AFNetwoeking上传视频
+ (void)uploadingVideWithUrl:(NSString *)url filePath:(NSString *)filePath params:(NSDictionary *)params Success:(void(^)(NSDictionary *respDic))success failure:(void(^)(NSString *error))failure;
{
AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager]initWithBaseURL:[NSURL URLWithString:url]];
//1.发送一个请求
[manager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:@"yyyy-MM-dd-HH:mm:ss"];
//日期
NSString * str = [formater stringFromDate:[NSDate date]];
//文件名
NSString * fileName = [NSString stringWithFormat:@"%@.mp4",str];
//data
NSData * fileData = [NSData dataWithContentsOfFile:filePath];
//multipartFile 和后台定的名称
[formData appendPartWithFileData:fileData name:@"multipartFile" fileName:fileName mimeType:@"video/quicktime"];
} progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@",responseObject);
if (success) {
success (responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//错误信息
NSLog(@"%@",error);
if (failure) {
failure (error.localizedDescription);
}
}];
}