iOS10、11 AVFoundation的一些改动

1、硬件设备获取

iOS10以后使用AVCaptureDeviceDiscoverySession获取手机的相关硬件设备

/*AVCaptureDeviceType
  AVCaptureDeviceTypeBuiltInMicrophone   麦克风
 AVCaptureDeviceTypeBuiltInWideAngleCamera 内置广角相机
 AVCaptureDeviceTypeBuiltInTelephotoCamera 内置长焦相机
 */
AVCaptureDeviceDiscoverySession *devicesIOS10 = [AVCaptureDeviceDiscoverySession  discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera] mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionUnspecified];
        
NSArray *devicesIOS  = devicesIOS10.devices;
 for (AVCaptureDevice *device in devicesIOS) {
      if ([device position] == position) {
         return device;
     }
 }

2、摄像机拍照功能实现

iOS10之后使用AVCapturePhotoOutput实现相机的拍照功能。

AVCaptureDevice *audioCatureDevice = nil;
        /*AVCaptureDeviceType
         AVCaptureDeviceTypeBuiltInMicrophone   麦克风
         AVCaptureDeviceTypeBuiltInWideAngleCamera 内置广角相机
         AVCaptureDeviceTypeBuiltInTelephotoCamera 内置长焦相机
         */
 //获取后置摄像头       
AVCaptureDeviceDiscoverySession *devicesIOS10 = [AVCaptureDeviceDiscoverySession  discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInMicrophone] mediaType:AVMediaTypeAudio position:AVCaptureDevicePositionUnspecified];
NSArray *devicesIOS  = devicesIOS10.devices;
if (devicesIOS.count>0) {
    audioCatureDevice = devicesIOS.firstObject;
}
    error = nil;
//初始化摄像头输入对象
    AVCaptureDeviceInput *audioCaptureDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:audioCatureDevice error:&error];
    if (error) {
        HJLog(@"获取设备输入对象时出现问题。");
        return;
    }
//将摄像头输入对象加入到aptureSession中
if ([self.captureSession canAddInput:self.captureDeviceInput]) {
        [self.captureSession addInput:self.captureDeviceInput];
    }
//初始化图片输出对象,并加入到aptureSession中
self.captureSession = [[AVCaptureSession alloc] init];
 self.captureImageOutputIOS10 = [[AVCapturePhotoOutput alloc] init];
 if ([self.captureSession canAddOutput:self.captureImageOutputIOS10]) {
        [self.captureSession addOutput:self.captureImageOutputIOS10];
   }
//初始化图像预览层。
    self.captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc]initWithSession:self.captureSession];
    self.captureVideoPreviewLayer.frame = self.view.bounds;
    self.captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [self.view.layer insertSublayer:self.captureVideoPreviewLayer atIndex:0];
    
    [self.captureSession startRunning];

capturePhotoWithSettings: delegate:方法为获取相机当前截图,AVCapturePhotoSettings对象在该方法每次调用时都应该从新创建,否则程序会崩溃。设置代理为当前控制器,并且在当前控制器实现代理方法。

 AVCaptureConnection *captureConnection = [self.captureImageOutputIOS10 connectionWithMediaType:AVMediaTypeVideo];
if (!captureConnection) {
      HJLog(@"拍照失败!");
      return;
}
//每次需重新创建
AVCapturePhotoSettings *setting = [AVCapturePhotoSettings photoSettings];
//闪光灯状态在此处设置
if (self.isFlash) {
      setting.flashMode = AVCaptureFlashModeOn;
 }else{
       setting.flashMode = AVCaptureFlashModeOff;
 }
 [self.captureImageOutputIOS10 capturePhotoWithSettings:setting delegate:self];

实现代理方法,获取相机拍摄图片(iOS 10到iOS 11)

#pragma mark -  captureOutput delegate
#ifdef __IPHONE_10_0
- (void)captureOutput:(AVCapturePhotoOutput *)captureOutput didFinishProcessingPhotoSampleBuffer:(nullable CMSampleBufferRef)photoSampleBuffer previewPhotoSampleBuffer:(nullable CMSampleBufferRef)previewPhotoSampleBuffer resolvedSettings:(AVCaptureResolvedPhotoSettings *)resolvedSettings bracketSettings:(nullable AVCaptureBracketedStillImageSettings *)bracketSettings error:(nullable NSError *)error{
    NSData *imageData = [AVCapturePhotoOutput  JPEGPhotoDataRepresentationForJPEGSampleBuffer:photoSampleBuffer previewPhotoSampleBuffer:previewPhotoSampleBuffer];
    self.photoShowView.image = [UIImage imageWithData:imageData];
    [self.captureSession stopRunning];
    
}
#endif

iOS 11以后获取拍摄图片方法

#ifdef __IPHONE_11_0
- (void)captureOutput:(AVCapturePhotoOutput *)output didFinishProcessingPhoto:(AVCapturePhoto *)photo error:(nullable NSError *)error{
    NSData *imageData = [photo  fileDataRepresentation];
    [self.captureSession stopRunning];
}

#endif

你可能感兴趣的:(iOS10、11 AVFoundation的一些改动)