2016-08-26 1 views
4

사진을 찍으려고 할 때 (전면 카메라), 사용자가 사진 모드에서 사진을 별도로 사용할 때만 오류가 발생합니다. apps 비디오. 사용자가 사진 속의 그림 속의 비디오를 가지고 있지 않다면 모든 것이 잘 작동합니다. 충돌이 줄에 발생합니다 오류iOS NSInternalInconsistencyException 앱이 iOS9 Picture in Picture 모드를 사용하는 동안 사진을 찍는 중

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '*** -[AVCaptureStillImageOutput captureStillImageAsynchronouslyFromConnection:completionHandler:] - inconsistent state.' 

내가 사진 모드에서 사진을 사용하는 동안 전화를 그냥 일반적으로 사진을 찍을 수없는 경우 확인 시도와

[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) { 

하지만 기본 아이폰 OS 카메라 앱은 사진을 잘 찍을 수 있습니다 (사진을 찍는 다른 방법을 사용하기는하지만). stillImageOutputvideoConnection은 잘 설정되어 있으며 0이 아닙니다.

다음은 도움이 될 수 있도록이 충돌을 일으키는 코드입니다.

avCaptureSession = [[AVCaptureSession alloc] init]; 
AVCaptureDevice* cameraDevice = [GS60_FriendFeed_ScreenshotSelfie_Preview_View frontFacingCameraIfAvailable]; 
avCaptureSession.sessionPreset = avCaptureSessionPresetString; 

NSError *error = nil; 
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:cameraDevice error:&error]; 
if (!input) { 
    NSLog(@"ERROR: trying to open camera: %@", error); 
} 
[avCaptureSession addInput:input]; 
AVCaptureStillImageOutput* stillImageOutput = [[AVCaptureStillImageOutput alloc] init]; 
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil]; 
[stillImageOutput setOutputSettings:outputSettings]; 
[avCaptureSession addOutput:stillImageOutput]; 
[avCaptureSession startRunning]; 

및 그림에서 그림이 우리가 할 수 없음을 감지하는 방법을 알고, 열려있는 동안이 방법을 사용할 수없는 경우 나중에

AVCaptureConnection* videoConnection = nil; 
AVCaptureStillImageOutput* stillImageOutput = [[avCaptureSession outputs] objectAtIndex:0]; 
for (AVCaptureConnection* connection in stillImageOutput.connections) { 
    for (AVCaptureInputPort *port in [connection inputPorts]) { 
     if ([[port mediaType] isEqual:AVMediaTypeVideo]) { 
      videoConnection = connection; 
      break; 
     } 
    } 
    if (videoConnection) { break; } 
} 

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; 
AVCaptureVideoOrientation avcaptureOrientation = AVCaptureVideoOrientationPortrait; 
if(orientation == UIInterfaceOrientationUnknown) { 
    avcaptureOrientation = AVCaptureVideoOrientationPortrait; 
} else if(orientation == UIInterfaceOrientationPortrait) { 
    avcaptureOrientation = AVCaptureVideoOrientationPortrait; 
} else if(orientation == UIInterfaceOrientationPortraitUpsideDown) { 
    avcaptureOrientation = AVCaptureVideoOrientationPortraitUpsideDown; 
} else if(orientation == UIInterfaceOrientationLandscapeLeft) { 
    avcaptureOrientation = AVCaptureVideoOrientationLandscapeLeft; 
} else if(orientation == UIInterfaceOrientationLandscapeRight) { 
    avcaptureOrientation = AVCaptureVideoOrientationLandscapeRight; 
} 
[videoConnection setVideoOrientation:avcaptureOrientation]; 

//this line flips the image so it uses exactly what the preview shows 
[videoConnection setVideoMirrored:YES]; 
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) { 
... 

나는하지만 사진을 찍을 수있을 것을 선호 그것을 가져 가면 여전히 도움이 될 것입니다.

도움 주셔서 감사합니다.

+1

해결 방법을 찾으셨습니까? – stevekohls

답변

2

연결이 있는지 확인하고

캡처 세션이 전화 통화, 알람에 의해 트리거 될 수있는 중단 및 시작 할 수 없을 때 일어날 수
if (!videoConnection || !videoConnection.enabled || !videoConnection.active) { 
    // Raise error here/warn user... 
    return; 
} 
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; 
AVCaptureVideoOrientation avcaptureOrientation = AVCaptureVideoOrientationPortrait; 
2

앱이 분할 화면에있는 활성화 iPad 또는 Picture-in-picture 모드를 사용하는 다른 응용 프로그램에서.

AVCaptureSessionWasInterrupted 알림을 위해 옵저버를 추가하면 앱이 응답하고 사용자에게 according to the reason을 알리는 것이 좋습니다.

당신은 알림 콜백에서 그 이유를 확인할 수 있습니다

notification.userInfo[AVCaptureSessionInterruptionReasonKey]; 

또한, 당신은 중단이 종료 할 때 세션을 다시 시작 AVCaptureSessionInterruptionEnded에 대한 관찰자를 추가해야합니다.

Apple has a great example 어떻게 작동하는지.