简体   繁体   中英

Why does this code work sometimes, but not others?

I created a 'mirror'-like view in my app that uses the front camera to show a 'mirror' to the user. The problem I'm having is that I have not touched this code in weeks (and it did work then) but now I'm testing it again and it's not working. The code is the same as before, there are no errors coming up, and the view in the storyboard is exactly the same as before. I have no idea what is going on, so I was hoping that this website would help.

Here is my code:

if([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]) {
        //If the front camera is available, show the camera


        AVCaptureSession *session = [[AVCaptureSession alloc] init];
        AVCaptureOutput *output = [[AVCaptureStillImageOutput alloc] init];
        [session addOutput:output];

        //Setup camera input
        NSArray *possibleDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
        //You could check for front or back camera here, but for simplicity just grab the first device
        AVCaptureDevice *device = [possibleDevices objectAtIndex:1];
        NSError *error = nil;
        // create an input and add it to the session
        AVCaptureDeviceInput* input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; //Handle errors

        //set the session preset
        session.sessionPreset = AVCaptureSessionPresetHigh; //Or other preset supported by the input device
        [session addInput:input];

        AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];
        //Now you can add this layer to a view of your view controller
        [cameraView.layer addSublayer:previewLayer];
        previewLayer.frame = self.cameraView.bounds;
        [session startRunning];
        if ([session isRunning]) {
            NSLog(@"The session is running");
        }
        if ([session isInterrupted]) {
            NSLog(@"The session has been interupted");
        }

    } else {
        //Tell the user they don't have a front facing camera
    }

Thank You in advanced.

Not sure if this is the problem but there is an inconsistency between your code and the comments. The inconsistency is with the following line of code:

AVCaptureDevice *device = [possibleDevices objectAtIndex:1];

In the comment above it says: "...for simplicity just grab the first device". However, the code is grabbing the second device, NSArray is indexed from 0. I believe the comment should be corrected as I think you are assuming the front camera will be the second device in the array.

If you are working on the assumption that the first device is the back camera and the second device is the front camera then this is a dangerous assumption. It would be much safer and more future proof to check the list of possibleDevices for the device that is the front camera.

The following code will enumerate the list of possibleDevices and create input using the front camera.

// Find the front camera and create an input and add it to the session
AVCaptureDeviceInput* input = nil;

for(AVCaptureDevice *device in possibleDevices) {
    if ([device position] == AVCaptureDevicePositionFront) {
        NSError *error = nil;

        input = [AVCaptureDeviceInput deviceInputWithDevice:device 
                                                      error:&error]; //Handle errors
        break;
    }
}

Update : I have just cut and pasted the code exactly as it is in the question into a simple project and it is working fine for me. I am seeing the video from the front camera. You should probably look elsewhere for the issue. First, I would be inclined to check the cameraView and associated layers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM