如何在iOS 9中使用AVCaptureSession实现Slide Over和Split View?

29

我的团队正在开发一套SDK,用于条形码扫描, 身份证扫描OCR。我们使用设备的相机,具体来说是AVCaptureSession,获取视频帧进行处理。

我们正在探索新的iOS 9多任务功能Slide Over和Split View。


Apple建议在以相机为中心的应用程序中选择退出这些功能,其中使用整个屏幕进行预览并快速捕捉瞬间是主要功能(参考)。这是他们在示例应用程序AVCam中使用的方法。
然而,我们的客户可能有不属于此类别的应用程序(例如移动银行应用程序),因此我们不能强制他们选择退出,而是需要处理SDK中的新功能。我们正在探索最佳方法来做到这一点,因为目前的文档并没有真正告诉我们该怎么做。
我们使用简单的相机示例应用程序来分析用例。该示例应用程序可在Github上获得,并且是基于iOS 9 Beta 5开发的。
从示例应用程序中,可以清楚地看到在使用Slide Over和Split View时发生了哪些系统事件。
- 当我们的应用程序为主要应用程序并使用Slide Over时,我们会收到UIApplicationWillResignActiveNotificationAVCaptureSessionDidStopRunningNotification - 当使用Slide Over并且我们的应用程序为次要应用程序时,我们会在此之后立即收到UIApplicationWillEnterForegroundNotificationAVCaptureSessionDidStopRunningNotification - 当使用Split View时,在每个分隔符拖动时,我们的应用程序都会收到UIApplicationWillResignActiveNotification。 - 但是,如果在Split View中启动相机,则会立即收到AVCaptureSessionDidStopRunningNotification 因此,经验证,当使用Slide Over或Split View时,AVCaptureSession会立即停止。
"What's confusing is that UIImagePickerController, which our sample app also supports, exhibits completely different behaviour. UIImagePickerController在应用程序进入Slide Over/ Split View时不会停止,而是完全正常地运行。用户可以在Split View中正常拍照。事实上,两个应用程序都可以同时使用UIImagePickerController,并且处于活动状态的应用程序的UIImagePickerController也是活动的。(您可以通过运行我们的示例应用程序和联系人应用程序 -> 新建联系人 -> 添加照片来尝试)"

考虑到这一切,我们的问题如下:

  • 如果在使用“Slide Over”和“Split View”时立即暂停AVCaptureSession,是否有必要监视AVCaptureSessionDidStopRunningNotification并向用户显示“摄像头已暂停”等消息,以便用户清楚地知道应用程序未执行扫描?

  • 为什么UIImagePickerController的行为与AVCaptureSession不同?

  • 我们可以期待苹果公司在将来的测试版本中改变AVCaptureSession的行为以匹配UIImagePickerController吗?


我正在开发一个应用程序,它需要使用相机预览,但这并不是一个以相机为中心的应用程序。你能否成功让你的应用程序在SplitView模式下工作?或者iOS根本不允许在SplitView中使用AVCaptureSession? - Joss
2个回答

21
如果您还没有发现。经过进一步调查,我现在可以回答您的第一个问题:
如果在使用Slide Over和Split View时AVCaptureSession立即暂停,则监视AVCaptureSessionDidStopRunningNotification并向用户显示“相机已暂停”消息是否明智,以便他清楚地知道应用程序未执行扫描?
您实际上要观察的通知是:AVCaptureSessionWasInterruptedNotification 您需要检查iOS9中新引入的原因:AVCaptureSessionInterruptionReason.VideoDeviceNotAvailableWithMultipleForegroundApps
override func viewWillAppear(animated: Bool)
{
    super.viewWillAppear(animated)
    self.addObserverForAVCaptureSessionWasInterrupted()
}

func addObserverForAVCaptureSessionWasInterrupted()
{
    let mainQueue = NSOperationQueue.mainQueue()
    NSNotificationCenter.defaultCenter().addObserverForName(AVCaptureSessionWasInterruptedNotification, object: nil, queue: mainQueue)
        { (notification: NSNotification) -> Void in

            guard let userInfo = notification.userInfo else
            {
                return
            }

            // Check if the current system is iOS9+ because AVCaptureSessionInterruptionReasonKey is iOS9+ (relates to Split View / Slide Over)
            if #available(iOS 9.0, *)
            {
                if let interruptionReason = userInfo[AVCaptureSessionInterruptionReasonKey] where Int(interruptionReason as! NSNumber) == AVCaptureSessionInterruptionReason.VideoDeviceNotAvailableWithMultipleForegroundApps.rawValue
                {
                    // Warn the user they need to get back to Full Screen Mode
                }
            }
            else
            {
                // Fallback on earlier versions. From iOS8 and below Split View and Slide Over don't exist, no need to handle anything then.
            }
        }
}

override func viewWillDisappear(animated: Bool)
{
    super.viewWillDisappear(true)

    NSNotificationCenter.defaultCenter().removeObserver(self)
}

你可以通过观察以下内容了解中断何时结束: AVCaptureSessionInterruptionEndedNotification 这是基于以下两个链接的答案: http://asciiwwdc.com/2015/sessions/211 https://developer.apple.com/library/ios/samplecode/AVCam/Introduction/Intro.html

1
通知已被重命名为 NSNotification.Name.AVCaptureSessionWasInterruptedNSNotification.Name.AVCaptureSessionInterruptionEnded - elmarko

1

iOS 16.0+以后,可以使用isMultitaskingCameraAccessEnabled标志。 参考

iOS 13.5+和iPadOS 13.5+以后,可以使用授权com.apple.developer.avfoundation.multitasking-camera-access,允许应用程序在与另一个前台应用程序并行运行时继续使用相机。 参考

有关在多任务处理期间访问相机的更多信息,请单击此处


1
isMultitaskingCameraAccessEnabled 总是返回 false。 - famfamfam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接