ios - 如何在 iOS 9 中使用带有 Slide Over 和 Split View 的 AVCaptureSession?

标签 ios camera multitasking avcapturesession ios9

我的团队正在为 barcode scanning 开发一套 SDK , ID scanningOCR .我们使用设备的摄像头,特别是 AVCaptureSession,来获取我们执行处理的视频帧。

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


Apple 建议为以相机为中心的应用程序选择退出这些功能,其中使用整个屏幕进行预览和快速捕捉瞬间是主要功能 (reference)。这是他们的示例应用程序中使用的方法 AVCam .

但是,我们的客户可能拥有不属于此类的应用程序(例如移动银行应用程序),因此我们不能强制他们选择退出,相反,我们需要处理 SDK 中的新功能。我们正在探索什么是最好的方法来做到这一点,因为目前的文档并没有真正告诉我们该怎么做。


我们使用简单的相机示例应用来分析用例。示例应用程序可在 Github 上获得它是从 iOS 9 Beta 5 开始开发的。

从示例应用程序中,可以清楚地看到使用 Slide Over 和使用 Split View 时发生了哪些系统事件。

  • 当我们的应用程序处于主要状态并使用 Slide Over 时,我们会得到 UIApplicationWillResignActiveNotificationAVCaptureSessionDidStopRunningNotification
  • 当使用 Slide Over 时,我们的应用程序是次要的,我们会立即收到 UIApplicationWillEnterForegroundNotificationAVCaptureSessionDidStopRunningNotification
  • 使用 Split View时,在每次拖动分隔线时,我们的应用都会收到 UIApplicationWillResignActiveNotification
  • 但是,如果在 Split View中启动相机,它会立即收到 AVCaptureSessionDidStopRunningNotification

因此,根据经验,看起来 AVCaptureSession 在使用 Slide Over 或 Split View 时会立即停止。

令人困惑的是 UIImagePickerController(我们的示例应用也支持)表现出完全不同的行为。

UIImagePickerController 在应用进入侧拉/ Split View时不会停止,相反,它会完全正常运行。人们通常可以在 Split View 中拍摄照片。事实上,两个都存在 UIImagePickerController 的应用程序可以并排工作,事件应用程序的 UIImagePickerController 处于事件状态。 (您可以通过运行我们的示例应用程序和联系人应用程序 -> 新建联系人 -> 添加照片来尝试)


考虑到所有这些,我们的问题如下:

  • 如果 AVCaptureSession 在使用 Slide Over 和 Split View 时立即暂停,那么监视 AVCaptureSessionDidStopRunningNotification 并显示一条消息“Camera Paused "给用户,让他清楚地知道应用程序没有执行扫描?

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

  • 我们是否可以期望 Apple 在未来的 AVCaptureSession 测试版中改变行为以匹配 UIImagePickerController

最佳答案

以防你还没有发现。经过更多调查后,我现在可以回答您的第一个问题:

If AVCaptureSession is immediately paused when Slide Over and Split View are used, is it a good idea to monitor AVCaptureSessionDidStopRunningNotification, and present a message "Camera Paused" to the user, so that he clearly knows that the app isn't performing scanning?

你真正想要观察的通知是这个: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

关于ios - 如何在 iOS 9 中使用带有 Slide Over 和 Split View 的 AVCaptureSession?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31915354/

相关文章:

ios - 使用 FacebookSDK 从异步 block 返回值的方法

android - fragment 在 PictureTake 上分离

android - 如何使用 Android CameraX API 缩放相机?

android - 适用于 iOS 和 Android 应用程序的相同 Firebase 实例

ios - 给分段控制带来阴影

android - 相机 MediaRecorder/CamcorderProfile

iphone - iOS 4 中的多任务感知应用程序和自定义 URL 方案

iOS - 确保在主线程上执行

iphone - 具有整数迭代的 applicationDidEnterBackground

ios - if语句ios检查json参数