Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the front camera in Swift?

Tags:

ios

swift

iphone

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?

like image 879
mindfreek Avatar asked Mar 19 '15 21:03

mindfreek


People also ask

How do I open the front camera in iOS Swift?

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.

How do you change camera in Swift?

Double Tap or Use Button To Switch Camera From Back to Front (Xcode 8, Swift 3)


2 Answers

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             }         }     } } 
like image 114
chrissukhram Avatar answered Sep 22 '22 23:09

chrissukhram


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.

like image 29
rickster Avatar answered Sep 21 '22 23:09

rickster