【问题标题】:iOS NSInternalInconsistencyException while taking a photo while app is using iOS9 picture in picture modeiOS NSInternalInconsistencyException 在应用程序在图片模式下使用 iOS9 图片时拍照
【发布时间】:2017-01-02 13:41:51
【问题描述】:

我在尝试拍照(前置摄像头)时遇到了崩溃,只有当用户将画中画模式用于单独的应用程序视频时才会失败。如果用户没有画中画视频,一切正常。崩溃发生在这一行:

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

我希望能够拍照,但如果在画中画打开时无法拍照,那么了解如何检测我们将无法拍照仍然会有所帮助。

感谢您的帮助。

【问题讨论】:

  • 你找到解决方案了吗?

标签: ios objective-c iphone avcapturesession


【解决方案1】:

确保连接存在并启用

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

【讨论】:

    【解决方案2】:

    当捕获会话中断且无法启动时,可能会发生这种情况,这可能由电话、警报、iPad 上的应用程序处于分屏状态或其他使用画中画模式的应用程序触发。

    AVCaptureSessionWasInterrupted 通知添加观察者是个好主意,这样您的应用就可以响应并提醒用户according to the reason

    您可以在通知回调中查看原因:

    notification.userInfo[AVCaptureSessionInterruptionReasonKey];
    

    此外,您应该为AVCaptureSessionInterruptionEnded 添加一个观察者,以便在中断结束时重新启动会话。

    Apple has a great example 了解其工作原理。

    【讨论】:

      猜你喜欢
      • 2016-04-11
      • 1970-01-01
      • 1970-01-01
      • 2016-02-25
      • 2012-12-25
      • 1970-01-01
      • 2013-12-27
      • 2023-04-04
      • 1970-01-01
      相关资源
      最近更新 更多