1

I'm writing an app which has a custom made view for taking photos with the Camera, similar to Apple's AVCam. In it, I want to make a button disappear and re-appear for the flash icon every time the camera is switched. IE When using the front camera, the flash button shouldn't be there and when using the back it should!

My code for this at the moment is:

  AVCaptureDevicePosition position = [[videoInput device] position];

    if (position == AVCaptureDevicePositionBack) {
  self.flashButton.hidden == YES;
}

But it comes up with an error on the videoInput and I'm not sure why... Any documentation you could direct me to or ideas for changes in my code would be much appreciated!

EDIT

Just basically specifically why does it come up with the error of 'use of undeclared identifier' with this code:

AVCaptureDevicePosition position = [[videoInput device] position];
falky
  • 589
  • 2
  • 11
  • 27

3 Answers3

2

The below code might help you :

AVCaptureDeviceInput *newVideoInput;
AVCaptureDevicePosition currentCameraPosition = [[videoInput device] position];

if (currentCameraPosition == AVCaptureDevicePositionBack)
{
    currentCameraPosition = AVCaptureDevicePositionFront;
}
else
{
    currentCameraPosition = AVCaptureDevicePositionBack;
}

AVCaptureDevice *backFacingCamera = nil;
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices) 
{
    if ([device position] == currentCameraPosition)
    {
        backFacingCamera = device;
    }
}
newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:backFacingCamera error:&error];
Priyank Gandhi
  • 1,257
  • 1
  • 16
  • 29
  • Thanks for your help but I saw that in the AVCam app. I don't know how to just write a small bit of code in pseudo code like: if current camera = back camera { show button } else { hide button} – falky May 09 '13 at 11:43
  • It comes up with an error every time I use [videoInput device]. So I was wondering if you had any idea or any other way to determine which camera is being used (either the 'AVCaptureDevicePositionBack' or 'AVCaptureDevicePositionFront') by my UIView. From there I will hide or show the flashButton...? Thanks for your help though. My idea was something like this: ' AVCaptureDevicePosition currentCameraPosition = [[videoInput device] position]; if ([currentCameraPosition isEqual:AVCaptureDevicePositionBack]){ self.flashButton.hidden = NO;} else {self.flashButton.hidden = YES; }' – falky May 09 '13 at 12:12
1

Swift 5.2

AVCaptureDevice is an enum. You can check it like this:

    var cameraPosition: AVCaptureDevice.Position!
    func switchCamera() {
        switch cameraPosition {
        case .back:
            cameraPosition = AVCaptureDevice.Position.front
            print(cameraPosition)
        case .front:
            cameraPosition = AVCaptureDevice.Position.back
            print(cameraPosition)
        default:
            cameraPosition = AVCaptureDevice.Position.front
            print(cameraPosition)

        }
Brody Higby
  • 137
  • 1
  • 8
0

I was looking for a solution for similar issue and came up with this and it might work for you (only tested in iOS8 and written in Swift):

var captureDevice : AVCaptureDevice?

...

var currentDevice:String = captureDevice?.localizedName as String!

if currentDevice.rangeOfString("Back Camera") != nil {
   //hide flash icon
} else if currentDevice.rangeOfString("Front Camera") != nil {
   //show flash icon
}

This code assumes that you already have setup the camera properly

Note: This may not be the best way because if Apple decides to change the localizedName it will break. And, I know this question is ancient but it might help someone else who stumbles on it

AppDever
  • 687
  • 1
  • 7
  • 17