[AVCaptureStillImageOutput captureStillImageAsynchronouslyFromConnection:completionHandler:] Inco...

近期在bug后臺收到一條NSInternalInconsistencyException導(dǎo)致閃退的記錄。

出錯堆棧如下:-[AVCaptureStillImageOutput captureStillImageAsynchronouslyFromConnection:completionHandler:] Inconsistent state

同時注意到該錯誤全部發(fā)生在iPad上。

最后查到原因是在iPad上開啟了畫中畫的情況下,使用AVCaptureSession拍照由于session處于被interrupt的狀態(tài),所以會導(dǎo)致閃退。還有其他的原因例如打電話,鬧鈴,處在分屏模式下等。

解決辦法:在session startRunning前添加AVCaptureSessionWasInterrupted通知的觀察。樣例代碼如下:


#pragma mark - Session Notifications

- (void) addObservers

{

    /*

    A session can only run when the app is full screen. It will be interrupted

    in a multi-app layout, introduced in iOS 9, see also the documentation of

    AVCaptureSessionInterruptionReason. Add observers to handle these session

    interruptions and show a preview is paused message. See the documentation

    of AVCaptureSessionWasInterruptedNotification for other interruption reasons.

    */

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sessionWasInterrupted:) name:AVCaptureSessionWasInterruptedNotification object:self.session];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sessionInterruptionEnded:) name:AVCaptureSessionInterruptionEndedNotification object:self.session];

}

- (void) removeObservers

{

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

- (void) sessionWasInterrupted:(NSNotification*)notification

{

    AVCaptureSessionInterruptionReason reason = [notification.userInfo[AVCaptureSessionInterruptionReasonKey] integerValue];

    NSLog(@"Capture session was interrupted with reason %ld", (long)reason);

    //在此禁止掉拍照功能,同時添加提醒

    if (reason == AVCaptureSessionInterruptionReasonAudioDeviceInUseByAnotherClient ||

        reason == AVCaptureSessionInterruptionReasonVideoDeviceInUseByAnotherClient) {

    }

    else if (reason == AVCaptureSessionInterruptionReasonVideoDeviceNotAvailableWithMultipleForegroundApps) {

    }

    else if (@available(iOS 11.1, *)) {

        if (reason == AVCaptureSessionInterruptionReasonVideoDeviceNotAvailableDueToSystemPressure) {

        }

    } else {

        // Fallback on earlier versions

    }

}

- (void) sessionInterruptionEnded:(NSNotification*)notification

{

  //在此可以恢復(fù)拍照功能

}

具體可以查看蘋果的樣例代碼

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容