簡體   English   中英

在應用程序使用iOS9圖片模式時拍攝照片時出現iOS NSInternalInconsistencyException

[英]iOS NSInternalInconsistencyException while taking a photo while app is using iOS9 picture in picture mode

我在嘗試拍攝照片時遇到了崩潰(前置攝像頭),只有在用戶使用圖片模式拍攝單獨的應用視頻時才會失敗。 如果用戶沒有圖片中的視頻,一切正常。 崩潰發生在這一行:

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

有錯誤

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

我嘗試檢查手機是否在使用圖片模式時無法拍攝照片,但默認的iOS相機應用程序可以拍攝照片(盡管它可能使用不同的拍照方法)。 stillImageOutputvideoConnection似乎設置得很好,並不是零。

以下是導致此崩潰的代碼,以防它有所幫助。

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) {
...

我希望能夠拍攝照片,但是如果圖片中的圖片是開放的那樣是不可能的,那么知道如何檢測到我們將無法拍攝它仍然會有所幫助。

謝謝你的幫助。

確保連接在那里並啟用

if (!videoConnection || !videoConnection.enabled || !videoConnection.active) {
    // Raise error here / warn user... 
    return;
}
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
AVCaptureVideoOrientation avcaptureOrientation = AVCaptureVideoOrientationPortrait;

這可能發生在捕獲會話中斷且無法啟動時,可以通過電話,鬧鍾,應用程序在iPad上的分屏或其他使用畫中畫模式的應用程序觸發。

AVCaptureSessionWasInterrupted通知添加觀察者是個好主意,這樣您的應用就可以根據原因響應並提醒用戶。

您可以在通知回調中檢查原因:

notification.userInfo[AVCaptureSessionInterruptionReasonKey];

此外,您應該為AVCaptureSessionInterruptionEnded添加一個觀察者,以便在中斷結束時重新啟動會話。

Apple就如何運作提供了一個很好的例子

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM