Skip to content

Instantly share code, notes, and snippets.

@steffex
Created January 23, 2014 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steffex/8588692 to your computer and use it in GitHub Desktop.
Save steffex/8588692 to your computer and use it in GitHub Desktop.
Method to request an AVCaptureDevice with a certain position. Method will fallback to the default AVCaptureDevice.
- (AVCaptureDevice *) getCaptureDeviceWithPosition:(AVCaptureDevicePosition)position
{
AVCaptureDevice *device;
// loop through all capture devices with video mediatype
for (AVCaptureDevice *d in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
check if the devices position is the desired one
if ([d position] == position) {
device = d;
}
}
// if we didnt get the desired device at position, lets go back to the default one
if (!device) {
device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
}
return device;
}
// USAGE:
// get the front facing camera:
AVCaptureDevice *device = [self getCaptureDeviceWithPosition:AVCaptureDevicePositionFront];
// get the back facing camera:
AVCaptureDevice *device = [self getCaptureDeviceWithPosition:AVCaptureDevicePositionBack];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment