01-AVFoundation捕捉

889 阅读14分钟

前言

本篇文章开始,将会介绍AVFoundation,苹果在iOS8.0之后推出的音视频框架,它提供了照片/视频捕捉功能,主要负责媒体文件的采集,只要是音视频相关的功能业务,例如小视频或直播,都离不开它。

一、核心类的介绍

首先介绍一下AVFoundation框架中的一些核心类👇🏻

  • 捕捉会话 AVCaptureSession
  • 捕捉设备 AVCaptureDevice
  • 捕捉设备输入 AVCaptureDeviceInput
  • 捕捉设备输出 AVCaptureOutput(抽象类) ◦ 静态图片 AVCaptureStillImageOutputQuickTime电影文件 AVCaputureMovieFileOutput ◦ 音频数据文件 AVCaputureAudioDataOutput ◦ 视频数据文件 AVCaputureVideoDataOutput
  • 捕捉连接 AVCaptureConnection
  • 捕捉预览 AVCaptureVideoPreviewLayer

接下来,实现一个基础案例,使用AVFoundation框架,实现一个相机的功能。

二、视频预览图层

首先,需要定义一个视频预览图层👉🏻 THPreviewView,当我们照相或拍视频时,需要预览吧,没有预览就是黑乎乎一片啥也看不到。THPreviewView是通过AVCaptureSession来关联AVCaptureVideoPreviewLayer(捕捉预览) 的,所以头文件中需要声明👇🏻

#import <AVFoundation/AVFoundation.h>

@interface THPreviewView : UIView
//session用来关联AVCaptureVideoPreviewLayer 和 激活AVCaptureSession
@property (strong, nonatomic) AVCaptureSession *session;
@end

然后THPreviewView.m实现文件中sessionget/set方法 👇🏻

- (AVCaptureSession*)session {
    //重写session方法,返回捕捉会话
    return [(AVCaptureVideoPreviewLayer*)self.layer session];
}

- (void)setSession:(AVCaptureSession *)session {
    //重写session属性的访问方法,在setSession:方法中访问视图layer属性。
    //AVCaptureVideoPreviewLayer 实例,并且设置AVCaptureSession 将捕捉数据直接输出到图层中,并确保与会话状态同步。
    [(AVCaptureVideoPreviewLayer*)self.layer setSession:session];
}

当然,我们还需要重写View的layerClass方法,指明是AVCaptureVideoPreviewLayer👇🏻

+ (Class)layerClass {
    //在UIView 重写layerClass 类方法可以让开发者创建视图实例自定义图层
    return [AVCaptureVideoPreviewLayer class];
}

2.1 坐标转换问题

当我们拍照时,需要聚焦,比如对人拍照,通常操作是 👉🏻 点击一下摄像头中的预览图层中的人的头部,那么问题来了👇🏻

⚠️ 点击屏幕是view的坐标,和摄像头所表示的坐标位置,不是同一个。

此时需要对坐标做个转换,将屏幕的坐标系转换成摄像头的坐标系,这里面涉及一些物理知识点:重力、镜像以及图层和方向等变化等因素,这里非常复杂,还好苹果系统提供了转化方法👇🏻

image.png

那么我们可以直接定义个私有的坐标系转换方法👇🏻

// 将屏幕坐标系上的触控点转换为摄像头上的坐标系点
- (CGPoint)captureDevicePointForPoint:(CGPoint)point {
    AVCaptureVideoPreviewLayer *layer =
        (AVCaptureVideoPreviewLayer *)self.layer;
    return [layer captureDevicePointOfInterestForPoint:point];
}

2.2 自定义手势事件

我们拍照时,通常需要聚焦、曝光等操作,仿照系统相机的手势,我们添加单击聚焦、双击曝光的手势👇🏻

  1. 在初始化时添加
    _singleTapRecognizer =
    [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];

    _doubleTapRecognizer =
    [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
    _doubleTapRecognizer.numberOfTapsRequired = 2;

   _doubleDoubleTapRecognizer =
    [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleDoubleTap:)];
    _doubleDoubleTapRecognizer.numberOfTapsRequired = 2;
    _doubleDoubleTapRecognizer.numberOfTouchesRequired = 2;

    [self addGestureRecognizer:_singleTapRecognizer];
    [self addGestureRecognizer:_doubleTapRecognizer];
    [self addGestureRecognizer:_doubleDoubleTapRecognizer];
    [_singleTapRecognizer requireGestureRecognizerToFail:_doubleTapRecognizer];
  1. 事件的实现👇🏻
- (void)handleSingleTap:(UIGestureRecognizer *)recognizer {
    CGPoint point = [recognizer locationInView:self];
    // self.focusBox是【聚焦】view,一个动画
    [self runBoxAnimationOnView:self.focusBox point:point]; 
    if (self.delegate) {
        [self.delegate tappedToFocusAtPoint:[self captureDevicePointForPoint:point]];
    }
}

- (void)handleDoubleTap:(UIGestureRecognizer *)recognizer {
    CGPoint point = [recognizer locationInView:self];
    // self.exposureBox是【曝光】view,一个动画
    [self runBoxAnimationOnView:self.exposureBox point:point];
    if (self.delegate) {
        [self.delegate tappedToExposeAtPoint:[self captureDevicePointForPoint:point]];
    }
}

- (void)handleDoubleDoubleTap:(UIGestureRecognizer *)recognizer {
    // 重置动画
    [self runResetAnimation];
    if (self.delegate) {
        [self.delegate tappedToResetFocusAndExposure];
    }
}

当然,这些事件会delegate出去👇🏻

@protocol THPreviewViewDelegate <NSObject>
- (void)tappedToFocusAtPoint:(CGPoint)point;//聚焦
- (void)tappedToExposeAtPoint:(CGPoint)point;//曝光
- (void)tappedToResetFocusAndExposure;//点击重置聚焦&曝光
@end

三、视频捕捉的配置

接下来,就是做一些视频/照片捕捉的配置工作。

3.1 设置Session

这些通常在viewController控制器页面内设置,例如我们在控制器THCameraController中声明AVCaptureSession捕捉会话 👇🏻

@interface THCameraController : NSObject
@property (nonatomic, strong, readonly) AVCaptureSession *captureSession;

// 用于设置、配置视频捕捉会话
- (BOOL)setupSession:(NSError **)error;
- (void)startSession;
- (void)stopSession;
@end

当然,捕捉离不开设备,你需要用摄像头捕捉视频,麦克风捕捉音频,就涉及到输入输出设备,通常将这些定义放在扩展类中👇🏻

@interface THCameraController () <AVCaptureFileOutputRecordingDelegate>

@property (strong, nonatomic) dispatch_queue_t videoQueue; //视频队列
@property (strong, nonatomic) AVCaptureSession *captureSession;// 捕捉会话
@property (weak, nonatomic) AVCaptureDeviceInput *activeVideoInput;//输入
@property (strong, nonatomic) AVCaptureStillImageOutput *imageOutput;
@property (strong, nonatomic) AVCaptureMovieFileOutput *movieOutput;
@property (strong, nonatomic) NSURL *outputURL;

@end

接下来就是实现👇🏻

- (BOOL)setupSession:(NSError **)error {

    
    //创建捕捉会话。AVCaptureSession 是捕捉场景的中心枢纽
    self.captureSession = [[AVCaptureSession alloc]init];
    
    /*
     AVCaptureSessionPresetHigh
     AVCaptureSessionPresetMedium
     AVCaptureSessionPresetLow
     AVCaptureSessionPreset640x480
     AVCaptureSessionPreset1280x720
     AVCaptureSessionPresetPhoto
     */
    //设置图像的分辨率
    self.captureSession.sessionPreset = AVCaptureSessionPresetHigh;
    
    //拿到默认视频捕捉设备 iOS系统返回后置摄像头
    AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    //将捕捉设备封装成AVCaptureDeviceInput
    //注意:为会话添加捕捉设备,必须将设备封装成AVCaptureDeviceInput对象
    AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:error];
    
    //判断videoInput是否有效
    if (videoInput)
    {
        //canAddInput:测试是否能被添加到会话中
        if ([self.captureSession canAddInput:videoInput])
        {
            //将videoInput 添加到 captureSession中
            [self.captureSession addInput:videoInput];
            self.activeVideoInput = videoInput;
        }
    }else
    {
        return NO;
    }
    
    //选择默认音频捕捉设备 即返回一个内置麦克风
    AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
    
    //为这个设备创建一个捕捉设备输入
    AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:error];
   
    //判断audioInput是否有效
    if (audioInput) {
        
        //canAddInput:测试是否能被添加到会话中
        if ([self.captureSession canAddInput:audioInput])
        {
            //将audioInput 添加到 captureSession中
            [self.captureSession addInput:audioInput];
        }
    }else
    {
        return NO;
    }

    //AVCaptureStillImageOutput 实例 从摄像头捕捉静态图片
    self.imageOutput = [[AVCaptureStillImageOutput alloc]init];
    
    //配置字典:希望捕捉到JPEG格式的图片
    self.imageOutput.outputSettings = @{AVVideoCodecKey:AVVideoCodecJPEG};
    
    //输出连接 判断是否可用,可用则添加到输出连接中去
    if ([self.captureSession canAddOutput:self.imageOutput])
    {
        [self.captureSession addOutput:self.imageOutput];
        
    }
    
    
    //创建一个AVCaptureMovieFileOutput 实例,用于将Quick Time 电影录制到文件系统
    self.movieOutput = [[AVCaptureMovieFileOutput alloc]init];
    
    //输出连接 判断是否可用,可用则添加到输出连接中去
    if ([self.captureSession canAddOutput:self.movieOutput])
    {
        [self.captureSession addOutput:self.movieOutput];
    }
    
    self.videoQueue = dispatch_queue_create("cc.VideoQueue", NULL);
    
    return YES;
}

以上【设置Session】的步骤大致有👇🏻

  1. 初始化
  2. 设置分辨率
  3. 配置输入设备 ◦ 注意转换为AVCaptureDeviceInput ◦ 包括视频输入和音频输入
  4. 配置输出 ◦ 静态图像输出 ◦ 视频文件输出
  5. 在为Session配置输入输出时 ◦ 注意一定要判断能否添加,摄像头或麦克风是公共设备,并不隶属于App
  6. 配置plist 👉🏻 案例中涉及摄像头、相册、麦克风,需要给用户提醒,处理隐私需求

3.2 开始&结束 Session

- (void)startSession {
    //检查是否处于运行状态
    if (![self.captureSession isRunning])
    {
        //使用同步调用会损耗一定的时间,则用异步的方式处理
        dispatch_async(self.videoQueue, ^{
            [self.captureSession startRunning];
        });
    }
}

- (void)stopSession {
    //检查是否处于运行状态
    if ([self.captureSession isRunning])
    {
        //使用异步方式,停止运行
        dispatch_async(self.videoQueue, ^{
            [self.captureSession stopRunning];
        });
    }
}

不难,有2点需要注意:

  1. 需要判断Session是否处于运行状态
  2. 异步方式开始或停止Session,同步的话会有耗时卡顿。

四、前后摄像头的改变

iOS系统会通常会有2个摄像头 👉🏻 前置和后置,默认通常是后置摄像头。(当然,x系列后 >2 个)。

4.1 准备方法

在做摄像头切换之前,需要写一些准备方法👇🏻

  • 摄像头的数量
- (NSUInteger)cameraCount {
     return [[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo] count];
}
  • 指定位置的摄像头设备
- (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition)position {
    //获取可用视频设备
    NSArray *devicess = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
    //遍历可用的视频设备 并返回position 参数值
    for (AVCaptureDevice *device in devicess)
    {
        if (device.position == position) {
            return device;
        }
    }
    return nil;
}
  • 激活与未激活的摄像头
- (AVCaptureDevice *)activeCamera {
    //返回当前捕捉会话对应的摄像头的device 属性
    return self.activeVideoInput.device;
}

//返回当前未激活的摄像头
- (AVCaptureDevice *)inactiveCamera {
    //通过查找当前激活摄像头的反向摄像头获得,如果设备只有1个摄像头,则返回nil
       AVCaptureDevice *device = nil;
      if (self.cameraCount > 1) {
          if ([self activeCamera].position == AVCaptureDevicePositionBack) {
              device = [self cameraWithPosition:AVCaptureDevicePositionFront];
          } else {
              device = [self cameraWithPosition:AVCaptureDevicePositionBack];
          }
     }
    return device;
}

4.2 切换摄像头

- (BOOL)switchCameras {
    //判断是否有多个摄像头
    if (![self canSwitchCameras])
    {
        return NO;
    }
    
    //获取当前设备的反向设备
    NSError *error;
    AVCaptureDevice *videoDevice = [self inactiveCamera];
    
    
    //将输入设备封装成AVCaptureDeviceInput
    AVCaptureDeviceInput *videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
    
    //判断videoInput 是否为nil
    if (videoInput)
    {
        //标注原配置变化开始
        [self.captureSession beginConfiguration];
        
        //将捕捉会话中,原本的捕捉输入设备移除
        [self.captureSession removeInput:self.activeVideoInput];
        
        //判断新的设备是否能加入
        if ([self.captureSession canAddInput:videoInput])
        {
            //能加入成功,则将videoInput 作为新的视频捕捉设备
            [self.captureSession addInput:videoInput];
            
            //将获得设备 改为 videoInput
            self.activeVideoInput = videoInput;
        }else
        {
            //如果新设备,无法加入。则将原本的视频捕捉设备重新加入到捕捉会话中
            [self.captureSession addInput:self.activeVideoInput];
        }
        
        //配置完成后, AVCaptureSession commitConfiguration 会分批的将所有变更整合在一起。
        [self.captureSession commitConfiguration];
    }else
    {
        //创建AVCaptureDeviceInput 出现错误,则通知委托来处理该错误
        [self.delegate deviceConfigurationFailedWithError:error];
        return NO;
    }
    return YES;
}

切换时有几点需要注意👇🏻

  • 判断是否有多个摄像头
  • 判断新的设备是否能加入
  • 出现错误,则delegate处理👇🏻
// 发生错误事件时,需要在对象委托上调用一些方法来处理
@protocol THCameraControllerDelegate <NSObject>
- (void)deviceConfigurationFailedWithError:(NSError *)error;// 设备错误
@end

五、摄像头自动聚焦

AVCaptureDevice 定义了很多方法,让开发者控制iOS设备上的摄像头,例如👇🏻

  • 可以独立调整和锁定摄像头的焦距、曝光、白平衡
  • 对焦和曝光可以基于特定的兴趣点进行设置,使其在应用中实现点击对焦、点击曝光的功能
  • 还可以让你控制设备的LED作为拍照的闪光灯手电筒的使用

5.1 功能是否支持

每当修改摄像头设备时,一定要先测试修改动作是否能被设备支持。并不是所有的摄像头都支持所有功能,例如前置摄像头就不支持对焦操作,因为它和目标距离一般在一臂之长的距离,但大部分后置摄像头是可以支持全尺寸对焦。

⚠️注意:尝试应用一个不被支持的动作,会导致异常崩溃

所以修改摄像头设备前,需要判断是否支持👇🏻

- (BOOL)cameraSupportsTapToFocus {
    //询问激活中的摄像头是否支持兴趣点对焦
    return [[self activeCamera]isFocusPointOfInterestSupported];
}

5.2 对焦

- (void)focusAtPoint:(CGPoint)point {
    //获取当前激活的摄像头设备
    AVCaptureDevice *device = [self activeCamera];
    //是否支持兴趣点对焦 & 是否自动对焦模式
    if (device.isFocusPointOfInterestSupported && [device isFocusModeSupported:AVCaptureFocusModeAutoFocus]) {
        NSError *error;
        //锁定设备准备配置,如果获得了锁
        if ([device lockForConfiguration:&error]) {
            //将focusPointOfInterest属性设置CGPoint
            device.focusPointOfInterest = point;
            //focusMode 设置为AVCaptureFocusModeAutoFocus
            device.focusMode = AVCaptureFocusModeAutoFocus;
            //释放该锁定
            [device unlockForConfiguration];
        }else{
            //错误时,则返回给错误处理代理
            [self.delegate deviceConfigurationFailedWithError:error];
        }
    }
}

⚠️注意:修改设备前,需要锁定lockForConfiguration:;修改完成后解锁unlockForConfiguration

六、摄像头自动曝光 & 锁定曝光

由于苹果对摄像头曝光这块逻辑的开放度有限制,再加上我们平时开发时会碰到一些对曝光有特殊需求的场景,此时需要我们另寻他法解决自动曝光锁定曝光

还是一样,需要判断设备是否支持曝光👇🏻

- (BOOL)cameraSupportsTapToExpose {
    //询问设备是否支持对一个兴趣点进行曝光
    return [[self activeCamera] isExposurePointOfInterestSupported];
}

接着就是对某一个坐标点进行曝光了👇🏻

static const NSString *THCameraAdjustingExposureContext;

- (void)exposeAtPoint:(CGPoint)point {
    //获取当前激活的摄像头设备
    AVCaptureDevice *device = [self activeCamera];
    //指定是AutoExposure【自动曝光】的mode
    AVCaptureExposureMode exposureMode =AVCaptureExposureModeContinuousAutoExposure;
    //判断是否支持【自动曝光】模式
    if (device.isExposurePointOfInterestSupported && [device isExposureModeSupported:exposureMode]) {
        NSError *error;
        //锁定设备准备配置
        if ([device lockForConfiguration:&error]) {
            //配置期望值
            device.exposurePointOfInterest = point;
            device.exposureMode = exposureMode;
            
            //判断设备是否支持【锁定曝光】的模式
            if ([device isExposureModeSupported:AVCaptureExposureModeLocked]) {
                //支持,则使用kvo确定设备的adjustingExposure属性的状态。
                [device addObserver:self forKeyPath:@"adjustingExposure" options:NSKeyValueObservingOptionNew context:&THCameraAdjustingExposureContext];
            }
            //释放该锁定
            [device unlockForConfiguration];
        } else {
            [self.delegate deviceConfigurationFailedWithError:error];
        }
    }
}

对焦一样,在修改前需要锁定,同时,我们看到,【锁定曝光】是通过KVO的方式观察device的adjustingExposure属性值,为什么使用KVO呢?

image.png

因为adjustingExposure属性是只读的,我们无法直接判断当前值的变化,所以使用KVO观察。

拿到新值后做了哪些逻辑处理呢?👇🏻

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context {

    //判断context(上下文)是否为THCameraAdjustingExposureContext
    if (context == &THCameraAdjustingExposureContext) {
        //获取device
        AVCaptureDevice *device = (AVCaptureDevice *)object;
        
        //判断设备是否不再调整曝光等级,确认设备的exposureMode是否可以设置为AVCaptureExposureModeLocked
        if(!device.isAdjustingExposure && [device isExposureModeSupported:AVCaptureExposureModeLocked]) {
            //移除作为adjustingExposure 的self,就不会得到后续变更的通知
            [object removeObserver:self forKeyPath:@"adjustingExposure" context:&THCameraAdjustingExposureContext];
            
            //异步方式调回主队列
            dispatch_async(dispatch_get_main_queue(), ^{
                NSError *error;
                if ([device lockForConfiguration:&error]) {
                    //修改exposureMode
                    device.exposureMode = AVCaptureExposureModeLocked;
                    //释放该锁定
                    [device unlockForConfiguration];
                } else {
                    [self.delegate deviceConfigurationFailedWithError:error];
                }
            });
        }
    } else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

KVO观察的逻辑其实就是 👉🏻 修改deviceexposureModeAVCaptureExposureModeLocked【锁定模式】

最后,我们再补充一个重置的方法👇🏻

//重新设置对焦&曝光
- (void)resetFocusAndExposureModes {
    AVCaptureDevice *device = [self activeCamera];
    AVCaptureFocusMode focusMode = AVCaptureFocusModeContinuousAutoFocus;
    
    //获取对焦兴趣点 和 连续自动对焦模式 是否被支持
    BOOL canResetFocus = [device isFocusPointOfInterestSupported]&& [device isFocusModeSupported:focusMode];
    AVCaptureExposureMode exposureMode = AVCaptureExposureModeContinuousAutoExposure;
    
    //确认曝光度可以被重设
    BOOL canResetExposure = [device isFocusPointOfInterestSupported] && [device isExposureModeSupported:exposureMode];
    
    //回顾一下,捕捉设备空间左上角(0,0),右下角(1,1) 中心点则(0.5,0.5)
    CGPoint centPoint = CGPointMake(0.5f, 0.5f);
    
    NSError *error;
    //锁定设备,准备配置
    if ([device lockForConfiguration:&error]) {
        //焦点可设,则修改
        if (canResetFocus) {
            device.focusMode = focusMode;
            device.focusPointOfInterest = centPoint;
        }
        
        //曝光度可设,则设置为期望的曝光模式
        if (canResetExposure) {
            device.exposureMode = exposureMode;
            device.exposurePointOfInterest = centPoint;
        }
        
        //释放锁定
        [device unlockForConfiguration];
    } else {
        [self.delegate deviceConfigurationFailedWithError:error];
    }
}

七、摄像头手电筒和闪光灯模式

手机LED灯的2种模式:手电筒和闪光灯。功能相关代码很简单👇🏻

//判断是否有闪光灯
- (BOOL)cameraHasFlash {
    return [[self activeCamera] hasFlash];
}

//闪光灯模式
- (AVCaptureFlashMode)flashMode {
    return [[self activeCamera] flashMode];
}

//设置闪光灯
- (void)setFlashMode:(AVCaptureFlashMode)flashMode {
    //获取激活的设备
    AVCaptureDevice *device = [self activeCamera];
    //判断是否支持闪光灯模式
    if ([device isFlashModeSupported:flashMode]) {
        //如果支持,则锁定设备
        NSError *error;
        if ([device lockForConfiguration:&error]) {
            //修改闪光灯模式
            device.flashMode = flashMode;
            //修改完成,解锁释放设备
            [device unlockForConfiguration];
        }else {
            [self.delegate deviceConfigurationFailedWithError:error];
        }
    }
}

//是否支持手电筒
- (BOOL)cameraHasTorch {
    return [[self activeCamera] hasTorch];
}

//手电筒模式
- (AVCaptureTorchMode)torchMode {
    return [[self activeCamera] torchMode];
}

//设置是否打开手电筒
- (void)setTorchMode:(AVCaptureTorchMode)torchMode {
    AVCaptureDevice *device = [self activeCamera];
    if ([device isTorchModeSupported:torchMode]) {
        NSError *error;
        if ([device lockForConfiguration:&error]) {
            device.torchMode = torchMode;
            [device unlockForConfiguration];
        } else {
            [self.delegate deviceConfigurationFailedWithError:error];
        }
    }
}

八、静态图片拍摄

比对系统相机,以上我们实现了一些摄像头的基础功能👇🏻

  • Session的配置,设置输入、输出设备
  • 辅助功能:切换前后摄像头,聚焦,曝光,手电筒和闪光灯等。

现在继续,我们实现一个静态图片的拍摄。一般需求是这样👇🏻

  1. 拍摄照片,完成后需保存到系统相册
  2. 提供一个缩略图,显示给user看

同时,拍摄静态图片时,还要考虑2点👇🏻

  1. 设备的方向问题:例如App只支持纵向时,也要支持横向拍摄
  2. 访问系统相册,需要修改plist 权限,否则会导致项目崩溃

8.1 方向

//获取方向值
- (AVCaptureVideoOrientation)currentVideoOrientation {
    AVCaptureVideoOrientation orientation;
    //获取UIDevice 的 orientation
    switch ([UIDevice currentDevice].orientation) {
        case UIDeviceOrientationPortrait:
            orientation = AVCaptureVideoOrientationPortrait;
            break;
        case UIDeviceOrientationLandscapeRight:
            orientation = AVCaptureVideoOrientationLandscapeLeft;
            break;
        case UIDeviceOrientationPortraitUpsideDown:
            orientation = AVCaptureVideoOrientationPortraitUpsideDown;
            break;
        default:
            orientation = AVCaptureVideoOrientationLandscapeRight;
            break;
    }
    
    return orientation;
}

8.2 拍摄图片

/*
    AVCaptureStillImageOutput 是AVCaptureOutput的子类。用于捕捉图片
 */
- (void)captureStillImage {
    //获取连接
    AVCaptureConnection *connection = [self.imageOutput connectionWithMediaType:AVMediaTypeVideo];
   
    //判断是否支持设置视频方向
    if (connection.isVideoOrientationSupported) {
        //获取方向值
        connection.videoOrientation = [self currentVideoOrientation];
    }
    
    //定义一个handler 块,会返回1个图片的NSData数据
    id handler = ^(CMSampleBufferRef sampleBuffer,NSError *error) {
        if (sampleBuffer != NULL) {
            NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:sampleBuffer];
            UIImage *image = [[UIImage alloc]initWithData:imageData];
            
            //重点:捕捉图片成功后,保存图片到系统相册
            [self writeImageToAssetsLibrary:image];
        } else {
            NSLog(@"NULL sampleBuffer:%@",[error localizedDescription]);
        }
    };
    
    //捕捉静态图片
    [self.imageOutput captureStillImageAsynchronouslyFromConnection:connection completionHandler:handler];
}

然后是保存相册的代码👇🏻

- (void)writeImageToAssetsLibrary:(UIImage *)image {
    //创建ALAssetsLibrary  实例
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc]init];
    
    //参数1:图片(参数为CGImageRef 所以image.CGImage)
    //参数2:方向参数 转为NSUInteger
    //参数3:写入成功、失败处理
    [library writeImageToSavedPhotosAlbum:image.CGImage
                              orientation:(NSUInteger)image.imageOrientation
                          completionBlock:^(NSURL *assetURL, NSError *error) {
        //成功后,发送捕捉图片通知。用于绘制程序的左下角的缩略图
        if (!error) {
            [self postThumbnailNotifification:image];
        } else {
            //失败打印错误信息
            id message = [error localizedDescription];
            NSLog(@"%@",message);
        }
    }];
}

发送缩略图通知👇🏻

- (void)postThumbnailNotifification:(UIImage *)image {
    //回到主队列
    dispatch_async(dispatch_get_main_queue(), ^{
        //发送请求
        NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
        [nc postNotificationName:THThumbnailCreatedNotification object:image];
    });
}

总结

本篇文章通过AVFoundation框架仿照实现系统相机的功能,主要涉及了AVCaptureSession的配置,前后摄像头切换,聚焦、曝光的处理和闪光灯、手电筒等辅助功能,都不难,但是有几点需要注意👇🏻

  1. 视频预览图层

    • 屏幕坐标系和摄像头坐标系的转换
  2. AVCaptureSession配置

    • 输入设备要转换成AVCaptureDeviceInput,且音频和视频的设备需要单独添加
    • 输出设备可直接添加
    • 添加前需判断canAddIn(Out)put
    • 配置info.plist,否则会导致项目崩溃
  3. 前后摄像头的改变

    • 改变前需beginConfiguration 改变后需commitConfiguration
    • 新的设备无法加入时,需要将原本的视频捕捉设备重新加入到捕捉会话中
  4. 聚焦、曝光

    • 需要判断是否支持聚焦、曝光等辅助功能
    • 改变前需要锁定设备准备配置lockForConfiguration:,改变后释放unlockForConfiguration
    • 锁定曝光是通过KVO观察adjustingExposure(只读属性)值改变时来设置
  5. 静态图片拍摄

    • 注意设备方向的判断
    • 保存系统相册时需要修改plist 权限,否则会导致项目崩溃