AVCaptureDevice A device that provides inputs (such as audio or video) for capturing sessions and controls for hardware-specific capture capabilities. An interface to the hardware, used to configure the properties of the underlying hardware (such as focus, flash, exposure, etc.). The underlying hardware includes the front camera, rear camera, microphone, flash, and so on. Use AVCaptureDevice to provide input data (such as audio or video) to AVCaptureSession objects.

useAVCaptureDeviceDiscoverySessionGet AVCaptureDevice

public convenience init(deviceTypes: [AVCaptureDevice.DeviceType], mediaType: AVMediaType? , position: AVCaptureDevice.Position) // Get available device var Devices: [AVCaptureDevice]]Copy the code
The extension AVCaptureDevice. DeviceType {@ the available (iOS 10.0. *) public static let builtInMicrophone: AVCaptureDevice. DeviceType @ the available (iOS 10.0. *) public static let builtInWideAngleCamera: AVCaptureDevice. DeviceType @ the available (iOS 10.0. *) public static let builtInTelephotoCamera: AVCaptureDevice. DeviceType @ the available (iOS 13.0. *) public static let builtInUltraWideCamera: AVCaptureDevice. DeviceType @ the available (iOS 10.2. *) public static let builtInDualCamera: AVCaptureDevice. DeviceType @ the available (iOS 13.0. *) public static let builtInDualWideCamera: AVCaptureDevice. DeviceType @ the available (iOS 13.0. *) public static let builtInTripleCamera: AVCaptureDevice. DeviceType @ the available (iOS 11.1. *) public static let builtInTrueDepthCamera: AVCaptureDevice. DeviceType @ the available (iOS, introduced: 10.0, deprecated: 10.2, the message: "Use AVCaptureDeviceTypeBuiltInDualCamera instead.") public static let builtInDuoCamera: AVCaptureDevice. DeviceType} @ the available (iOS 4.0. *) public enum Position: Int { case unspecified = 0 case back = 1 case front = 2 }Copy the code

AVCaptureDevice DeviceType enumeration

The enumeration name explain
builtInMicrophone Built-in microphone. (Microphone)
builtInWideAngleCamera Built-in wide Angle camera. (wide)
builtInTelephotoCamera The built-in camera device has a longer focal length than the wide-angle camera. (long)
builtInUltraWideCamera The built-in camera has a shorter focal length than a wide-angle camera. (Ultra Wide Angle)
builtInDualCamera Combination of wide-angle camera and telephoto camera (dual wide-angle (telephoto))
builtInDualWideCamera A device consisting of two fixed focal length cameras, (one ultra wide Angle + one wide Angle)
builtInTripleCamera A device consisting of three fixed focal length cameras (ultra wide Angle + wide Angle + telephoto)
builtInTrueDepthCamera A combination of cameras and other sensors to create capture devices capable of capturing photos, video and depth.
builtInDuoCamera Not recommended after iOS 10.2
The enumeration name explain
unspecified Is not specified
back Rear camera
front Front facing camera

How to use

Camera initialization (AVCaptureDeviceInput)

AVCaptureDevice *videoDevice; if (videoDevice == nil) { NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; for (AVCaptureDevice *device in devices) { if ([device position] == cameraPosition) { videoDevice = device; } } _isWideCamera = NO; } _inputCamera = videoDevice; if (! _inputCamera) { return nil; } // Create the capture session _captureSession = [[AVCaptureSession alloc] init]; [_captureSession beginConfiguration]; // Add the video input NSError *error = nil; videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:_inputCamera error:&error]; if ([_captureSession canAddInput:videoInput]) { [_captureSession addInput:videoInput]; }Copy the code

Toggle camera type -> BuiltInTripleCamera

BOOL isRunning = self.captureSession.isRunning; if (isRunning) [self.captureSession stopRunning]; BOOL isUpdateImageOrientation = curDevicePosition ! = _inputCamera.position; AVCaptureDevice *videoDevice; If (@available (iOS 13.0, *)) {/// if this mode is supported // Cut three-shot full open mode videoDevice = [AVCaptureDevice defaultDeviceWithDeviceType:AVCaptureDeviceTypeBuiltInTripleCamera mediaType:AVMediaTypeVideo position:curDevicePosition]; } [_captureSession beginConfiguration]; _inputCamera = videoDevice; NSError *error; AVCaptureDeviceInput * newvideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:_inputCamera error:&error]; if (newvideoInput ! = nil { [_captureSession removeInput:videoInput]; if ([_captureSession canAddInput:newvideoInput]) { [_captureSession addInput:newvideoInput]; videoInput = newvideoInput; } else { [_captureSession addInput:videoInput]; } } [_captureSession commitConfiguration]; ActiveFamat */ if (isRunning) [self.captureSession startRunning];Copy the code