I am trying to get the front camera with live view. I am able to get the back camera using:
var backCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
But I can't seem to find how to get the front camera. How can I change the code above to use the front camera?
Basic Swift Code for iOS Apps We'll do it in a series of steps. Filter out the front camera if exists. The devices() method of AVCapture returns the list of cameras available. From that list of the camera, we'll use the filter function in which we'll check if the position is a camera in front or not.
Double Tap or Use Button To Switch Camera From Back to Front (Xcode 8, Swift 3)
Here is a working example from one of my projects to get the front camera. This is in objective-c but proven to work and easy enough to convert to swift.
NSArray *videoDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; AVCaptureDevice *captureDevice = nil; for (AVCaptureDevice *device in videoDevices){ if (device.position == AVCaptureDevicePositionFront){ captureDevice = device; break; } }
And in Swift 3.2+:
if let videoDevices = AVCaptureDevice.devices(withMediaType: AVMediaTypeVideo) { var captureDevice: AVCaptureDevice for device in videoDevices { if let device = device as? AVCaptureDevice { if device.position == AVCaptureDevicePosition.front { captureDevice = device break } } } }
In iOS 10.0 and later, you don't need to iterate through AVCaptureDevice
.devices
(or devicesWithMediaType
) to find a camera by position. (In fact, both of those APIs are deprecated in iOS 10, and don't return the full set of available devices on iPhone 7 Plus, iPhone 8 Plus, or iPhone X.)
If you just need to find a single device based on simple characteristics (like a front-facing camera that can shoot video), just use AVCaptureDevice
.defaultDevice(withDeviceType:mediaType:position:)
. For example:
guard let device = AVCaptureDevice.defaultDevice( withDeviceType: .builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: .front) else { fatalError("no front camera. but don't all iOS 10 devices have them?") // then use the device: captureSession.addInput(device) or whatever
Really that's all there is to it for most use cases.
There's also AVCaptureDeviceDiscoverySession
as a replacement for the old method of iterating through the devices
array. However, most of the things you'd usually iterate through the devices
array for can be found using the new defaultDevice(withDeviceType:mediaType:position:)
method, so you might as well use that and write less code.
The cases where AVCaptureDeviceDiscoverySession
is worth using are the less common, more complicated cases: say you want to find all the devices that support a certain frame rate, or use key-value observing to see when the set of available devices changes.
By the way, Apple also has a guide to the iOS 10 / Swift 3 photo capture system and some sample code that both show current best practices for these APIs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With