iOS拍照,视频录制-简单使用

引言

在iOS中使用摄像头拍照,录制视频有两种方式,

  • 使用系统现成的UIImagePickerController控件
  • 使用AVFoundation框架中的一些类自定义

本篇博客我们先来使用系统现成的UIImagePickerController控件完成拍照,视频录制功能。

要用UIImagePickerController来拍照或者录制视频通常可以分为如下步骤:

  • 创建UIImagePickerController对象。

  • 指定拾取源,平时选择照片时使用的拾取源是照片库或者相簿,此刻需要指定为摄像头类型。

  • 指定摄像头,前置摄像头或者后置摄像头。

  • 设置媒体类型mediaType,注意如果是录像必须设置,如果是拍照此步骤可以省略,因为mediaType默认包含kUTTypeImage(注意媒体类型定义在MobileCoreServices.framework中)

  • 指定捕获模式,拍照或者录制视频。(视频录制时必须先设置媒体类型再设置捕获模式

  • 展示UIImagePickerController(通常以模态窗口形式打开)。

  • 拍照和录制视频结束后在代理方法中展示/保存照片或视频。

当然这个过程中有很多细节可以设置,例如是否显示拍照控制面板,拍照后是否允许编辑等等,通过上面的属性/方法列表相信并不难理解。下面就以一个示例展示如何使用UIImagePickerController来拍照和录制视频,下面的程序中只要将_isVideo设置为YES就是视频录制模式,录制完后在主视图控制器中自动播放;如果将_isVideo设置为NO则为拍照模式,拍照完成之后在主视图控制器中显示拍摄的照片:

#import "AVImageVideoSystemViewController.h"
#import 
#import 

@interface AVImageVideoSystemViewController ()

@property (nonatomic, strong) UIImagePickerController * pickerController;

@property (nonatomic, strong) AVPlayer * player;

@property (nonatomic, assign) BOOL isVideo;

@property (nonatomic, strong) UIImageView * showImageView;

@end

@implementation AVImageVideoSystemViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"系统控件拍照和录像";
    
    _isVideo = YES;
    
    [self setupUI];
}


#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary *)editingInfo{

}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

    NSString * mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) { //如果是拍照
        UIImage * image;
        //如果允许编辑则获得编辑后的照片,否则获取原始照片
        if (_pickerController.allowsEditing) {
            image = [info objectForKey:UIImagePickerControllerEditedImage];//获取编辑后的照片
        } else {
            image = [info objectForKey:UIImagePickerControllerOriginalImage];//获取原始照片
        }
        
        [self.showImageView setImage:image];
        
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);//保存到相簿
        
    } else if([mediaType isEqualToString:(NSString *)kUTTypeMovie]){ //如果是录制视频
        NSURL * url = [info objectForKey:UIImagePickerControllerMediaURL];
        NSString * urlStr = [url path];
        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(urlStr)) {
            //保存视频到相簿,注意也可以使用ALAssetsLibrary来保存
            UISaveVideoAtPathToSavedPhotosAlbum(urlStr, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
        }
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{

    NSLog(@"放弃");
    [self dismissViewControllerAnimated:YES completion:nil];

}

#pragma mark - 点击方法
- (void)clickButton:(UIButton *)sender{

    [self presentViewController:self.pickerController animated:YES completion:nil];
}

#pragma mark - 自定义方法
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{

    if (error) {
        NSLog(@"保存视频过程中发生错误,错误信息:%@",error.localizedDescription);
    } else {
        NSLog(@"视频保存成功");
        //录制完后自动播放
        NSURL * url = [NSURL fileURLWithPath:videoPath];
        _player = [AVPlayer playerWithURL:url];
        AVPlayerLayer * playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
        playerLayer.frame = self.showImageView.bounds;
        [self.showImageView.layer addSublayer:playerLayer];
        [_player play];
    }
}

- (void)setupUI{

    [self.view addSubview:self.showImageView];
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"拍录" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    button.frame = CGRectMake(100, 500, 60, 60);
    button.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height-60);
    button.layer.cornerRadius = 30;
    button.layer.masksToBounds = YES;
    [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

- (UIImagePickerController *)pickerController{
    if (!_pickerController) {
        _pickerController = [[UIImagePickerController alloc] init];
        //设置_pickerController的来源,这里设置为摄像头。
        /*
         UIImagePickerControllerSourceTypePhotoLibrary:照片库
         ,默认值
         UIImagePickerControllerSourceTypeCamera:摄像头
         UIImagePickerControllerSourceTypeSavedPhotosAlbum:相
         */
        _pickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        //设置摄像头类型
        /*
         UIImagePickerControllerCameraDeviceRear,//后置摄像头
         UIImagePickerControllerCameraDeviceFront //前置摄像头
         */
        _pickerController.cameraDevice = UIImagePickerControllerCameraDeviceRear;
        
        if (_isVideo) {//设置媒体类型为视频,默认为kUTTypeImage;
            _pickerController.mediaTypes = @[(NSString *)kUTTypeMovie];
            _pickerController.videoQuality = UIImagePickerControllerQualityTypeIFrame1280x720;
            _pickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
            
        }else{//设置摄像头捕获模式为 UIImagePickerControllerCameraCaptureModePhoto(默认就是)
            
            _pickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
        }
        
        _pickerController.allowsEditing = YES; // 允许编辑
        _pickerController.delegate = self; //设置代理,检测操作
    }
    return _pickerController;
}

- (UIImageView *)showImageView{
    if (!_showImageView) {
        _showImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 375, self.view.bounds.size.height-100)];
        _showImageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _showImageView;
}

@end


iOS拍照,视频录制-简单使用_第1张图片
效果

你可能感兴趣的:(iOS拍照,视频录制-简单使用)