-1

나는 프론트 카메라를 사용하여 사용자에게 '거울'을 보여주는 '거울'과 같은 뷰를 내 앱에서 만들었습니다. 내가 겪고있는 문제는 몇 주 후에이 코드를 건드리지 않았다는 것입니다.하지만 지금은 다시 테스트하고 작동하지 않습니다. 코드는 이전과 동일하며 오류가 발생하지 않으며 스토리 보드의보기는 이전과 완전히 동일합니다. 나는 무슨 일이 일어나고 있는지 전혀 모른다. 그래서 나는이 웹 사이트가 도움이되기를 바랬다. 이 코드는 때때로 작동하지만 다른 코드는 작동하지 않는 이유는 무엇입니까?

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 
    } 

고급에 감사 :

여기 내 코드입니다.

답변

5

이것이 문제이지만 코드와 주석이 일치하지 않는지 확실하지 않습니다. 불일치는 다음 코드 줄 함께 : 주석에서

AVCaptureDevice *device = [possibleDevices objectAtIndex:1]; 

는 말한다 위 "... 단순화를 위해 단지 첫 번째 장치를 잡아." 그러나 코드는 두 번째 장치를 잡으려고합니다. NSArray은 0부터 인덱싱됩니다. 나는 앞 카메라가 배열의 두 번째 장치가 될 것으로 가정하므로 주석을 수정해야한다고 생각합니다.

첫 번째 장치가 뒷쪽 카메라이고 두 번째 장치가 앞쪽 카메라라는 가정하에 작업하는 경우 이는 위험한 가정입니다. 전면 카메라 인 장치의 possibleDevices 목록을 확인하는 것이 훨씬 안전하고 미래의 증거가 될 것입니다.

다음 코드는 possibleDevices의 목록을 열거하고 앞 카메라를 사용하여 input을 만듭니다.

// 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; 
    } 
} 

업데이트 : 난 그냥 잘라하고 간단한 프로젝트로 질문에 정확하게 코드를 붙여 그것은 나를 위해 잘 작동했다. 전면 카메라에서 비디오를 볼 수 있습니다. 아마 다른 곳에서 그 문제를 찾아야합니다. 먼저 cameraView 및 관련 레이어를 확인하려고합니다.

+0

나는 그걸 또한했고, 여전히 아무것도 보이지 않습니다. 너는 무엇을 시험하고 있니? 그것이 iOS 6 GM를 달리게 할 수 있으면 나는 달리게한다. –

+0

또한 'cameraView'도 확인했는데 UIView 일뿐입니다. 나는 또한 내 코드를 깨뜨렸고 전체 코드를 거쳐서 무언가를 잊어 버리고있는 것처럼 보입니다 - 나는 단지 무엇을 모르겠습니다. –

+0

GM을 실행하는 iPhone 4s에서 질문의 코드를 실행하여 정상적으로 작동했습니다. – mttrb