如何在iOS 9中使用AVCaptureSession与Slide Over和Split View? [英] How to use AVCaptureSession with Slide Over and Split View in iOS 9?

查看:516
本文介绍了如何在iOS 9中使用AVCaptureSession与Slide Over和Split View?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的小组正在开发一组SDK,用于条形码扫描 ID扫描 OCR 。我们使用设备的相机,具体来说, AVCaptureSession ,以获取我们执行我们的处理的视频帧。



重新探索新的iOS 9多任务功能滑动和分割视图。






Apple建议选择退出这些功能应用,其中使用整个屏幕进行预览和快速捕获时间是一个主要功能(参考)。这是在其示例应用中使用的方法 AVCam



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






我们使用我们简单的Camera示例应用来分析用例。示例应用程序位于 Github 上,其版本为iOS 9 Beta 5。



从示例应用程序中,可以清楚地看到使用幻灯片时和使用拆分视图时发生的系统事件。




  • 当我们的应用程式为主要应用程式时,使用Slide Over,我们会收到 UIApplicationWillResignActiveNotification AVCaptureSessionDidStopRunningNotification
  • 使用Slide Over时,我们的应用程序是次要的,我们获得 UIApplicationWillEnterForegroundNotification AVCaptureSessionDidStopRunningNotification

  • 使用Split View时,在每个分隔拖动上,我们的应用程序会获得 UIApplicationWillResignActiveNotification

  • 但是,如果在Split View中启动相机,则会立即获得 AVCaptureSessionDidStopRunningNotification



所以,根据经验,当使用Slide Over或Split View时,看起来像 AVCaptureSession 会立即停止。



令人困惑的是,我们的示例应用程序还支持的 UIImagePickerController 表现出完全不同的行为。

当应用程序进入Slide Over / Split View时,UIImagePickerController 不会停止,通常可以在分割视图中拍摄照片。事实上,两个应用程序都提供 UIImagePickerController ,可以并排工作,其中 UIImagePickerController 活动。 (您可以通过运行我们的示例应用程序,然后通过联系人应用程序 - >新建联系人 - >添加照片)来尝试。






所有这一切,我们的问题如下:




  • 如果 AVCaptureSession 在使用Slide Over和Split View时立即暂停,是否最好监视 AVCaptureSessionDidStopRunningNotification ,并向用户显示一条消息Camera Paused,以便他


  • 为什么 UIImagePickerController 的行为不同于 AVCaptureSession


  • AVCaptureSession 更改以匹配 UIImagePickerController



解决方案

如果你还没有找到答案。经过一些更多的调查后,我现在可以回答你的第一个问题:


如果AVCaptureSession在幻灯片和分割
视图使用,是一个好主意,监视
AVCaptureSessionDidStopRunningNotification,并向用户显示一条消息
Camera Paused,这样他清楚地知道应用程序
没有执行扫描? / p>

您实际想要观察的通知是这样的: 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)
{ ) - >无效

guard让userInfo = notification.userInfo else
{
return
}

//检查当前系统是否为iOS9 +因为AVCaptureSessionInterruptionReasonKey是iOS9 +(涉及分割视图/幻灯片)
如果#available(iOS 9.0,*)
{
如果允许interrupReason = userInfo [AVCaptureSessionInterruptionReasonKey] )== AVCaptureSessionInterruptionReason.VideoDeviceNotAvailableWithMultipleForegroundApps.rawValue
{
//警告他们需要返回全屏模式的用户
}
}
else
{
//早期版本的后备。从iOS8和以下的分割视图和幻灯片不存在,没有必要处理任何东西。
}
}
}

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


My team is developing a set of SDKs for barcode scanning, ID scanning and OCR. We use device's camera, specifically, AVCaptureSession, to obtain video frames on which we perform our processing.

We're exploring new iOS 9 multitasking features Slide Over and Split View.


Apple suggests opting out of these features for camera-centric apps, where using the entire screen for preview and capturing a moment quickly is a primary feature (reference). This is the approach the use in their sample app AVCam.

However, our customers might have apps which don't fall into this category (e.g Mobile banking apps), so we cannot force them to opt out, instead, we need to handle new features in the SDK. We're exploring what would be the best approach to do that, since the docs at the moment aren't really telling us what to do.


We used our simple Camera sample app to analyse the use case. The sample app is available on Github and it's developed as of iOS 9 Beta 5.

From the sample app, it can be clearly seen which system events happen when Slide Over is used, and when Split View is used.

  • When our app is primary, and Slide Over is used, we get UIApplicationWillResignActiveNotification and AVCaptureSessionDidStopRunningNotification
  • When Slide Over is used, and our app is secondary, we get UIApplicationWillEnterForegroundNotification and AVCaptureSessionDidStopRunningNotification immediately after that
  • When Split View is used, on each divider drag, our app gets UIApplicationWillResignActiveNotification.
  • However, if the Camera is launched when in Split View, it immediately gets AVCaptureSessionDidStopRunningNotification

So, empirically, it looks like AVCaptureSession is immediately stopped when Slide Over or Split View are used.

What's confusing is that UIImagePickerController, which our sample app also supports, exhibits completely different behaviour.

UIImagePickerController isn't stopped when the app goes into Slide Over/ Split View, instead, it functions completely normally. One can normally take a photo in Split View. In fact, two apps, both of which present UIImagePickerController, can work side by side, with UIImagePickerController of the active app being active. (You can try that by running our sample app, and Contacts app -> New Contact -> Add photo)


With all this in mind, our questions are the following:

  • 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?

  • Why is behaviour of UIImagePickerController different than AVCaptureSession?

  • Can we expect from Apple than in future beta versions behaviour of AVCaptureSession changes to match UIImagePickerController?

解决方案

In case you haven't found out yet. After some more investigation I can now answer your first question:

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?

The notification you actually want to observe is this one: AVCaptureSessionWasInterruptedNotification

And you want to check for the newly introduced in iOS9 reason: 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)
}

You can also know when the interruption was ended by observing: AVCaptureSessionInterruptionEndedNotification

Answer based on these two links:

http://asciiwwdc.com/2015/sessions/211 https://developer.apple.com/library/ios/samplecode/AVCam/Introduction/Intro.html

这篇关于如何在iOS 9中使用AVCaptureSession与Slide Over和Split View?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆