iOS ARKitARKIT

07-ARSession介绍

2017-06-22  本文已影响174人  60916fc63567

7.1 概述###

ARSession是一个连接底层与AR视图之间的桥梁,其实ARSCNView内部所有的代理方法都是由ARSession来提供的。
ARSession与ARScnView之间的关系看起来是这样的:

image.png

7.2 ARSession获取相机位置数据主要有两种方式###

第一种:push。 实时不断的获取相机位置,由ARSession主动告知用户。通过实现ARSession的代理-
(void)session:(ARSession *)session didUpdateFrame:(ARFrame *)frame来获取
第二种:pull。 用户想要时,主动去获取。ARSession的属性currentFrame来获取

7.3 API介绍(swift版本):###

<pre>
import ARKit

extension ARSession {

/**回话断开重连时的行为
 Set of options for running the session.
 @discussion These options alter the behavior of calling run on a session.
 Providing no options will result in the default behavior of resuming tracking
 from the last known position and keeping all existing anchors.
 */
@available(iOS 11.0, *)
public struct RunOptions : OptionSet {

    public init(rawValue: UInt)

    
    /** The session will reset tracking.表示重置追踪 */
    public static var resetTracking: ARSession.RunOptions { get }

    
    /** The session will remove existing anchors. 移除现有锚点*/
    public static var removeExistingAnchors: ARSession.RunOptions { get }
}

}

/**
The ARSession class configures and runs different Augmented Reality techniques on a device.
*/
@available(iOS 11.0, *)
open class ARSession : NSObject {

/**
代理
 A delegate for receiving ARSession updates.
 */
weak open var delegate: ARSessionDelegate?


/**
 指定代理执行的线程(主线程不会有延迟,子线程会有延迟),不指定的话默认主线程 
 The dispatch queue on which the delegate calls are performed.
 @discussion If not provided or nil, delegate calls will be performed on the main queue.
 */
open var delegateQueue: DispatchQueue?


/**相机当前的位置、时间戳、视频帧信息(是由回话追踪配置计算出来的)
 The current frame of the session.
 */
@NSCopying open var currentFrame: ARFrame? { get }


/**会话追踪配置
 The configuration currently being used by the session.
 */
@NSCopying open var configuration: ARSessionConfiguration? { get }


/**运行会话
 Runs the session with the provided configuration and options.
 @discussion Calling run on a session that has already started will
 transition immediately to using the new session configuration. Options
 can be used to alter the default behavior when transitioning configurations.
 @param configuration The configuration to use.
 @param options The run options to use.
 */
open func run(_ configuration: ARSessionConfiguration, options: ARSession.RunOptions = [])


/**
 暂停回话
 Pauses the session.
 @discussion Once paused, no more updates will be received from the
 session until run is called again.
 */
open func pause()


/**
 添加锚点
 Adds an anchor to the session.
 @discussion The anchor will be added in the next frame update.
 @param anchor The anchor to add.
 */
open func add(anchor: ARAnchor)


/**
 移除锚点
 Removes an anchor from the session.
 @discussion The anchor will be removed from subsequent frame updates.
 @param anchor The anchor to remove.
 */
open func remove(anchor: ARAnchor)

}

@available(iOS 11.0, *)
public protocol ARSessionObserver : NSObjectProtocol {

/**
 This is called when a session fails.
session失败
 @discussion On failure the session will be paused.
 @param session The session that failed.
 @param error The error being reported (see ARError.h).
 */
optional public func session(_ session: ARSession, didFailWithError error: Error)


/**
 This is called when the camera's tracking state has changed.
 相机改变追踪状态
 @param session The session being run.
 @param camera The camera that changed tracking states.
 */
optional public func session(_ session: ARSession, cameraDidChangeTrackingState camera: ARCamera)


/**
 This is called when a session is interrupted.
 session意外断开(如果开启ARSession之后,APP退到后台就有可能导致回话断开)
 @discussion A session will be interrupted and no longer able to track when
 it fails to receive required sensor data. This happens when video capture is interrupted,
 for example when the application is sent to the background or when there are
 multiple foreground applications (see AVCaptureSessionInterruptionReason).
 No additional frame updates will be delivered until the interruption has ended.
 @param session The session that was interrupted.
 */
optional public func sessionWasInterrupted(_ session: ARSession)


/**
 This is called when a session interruption has ended.
 session会话断开恢复(短时间退到后台再进入APP会自动恢复)
 @discussion A session will continue running from the last known state once
 the interruption has ended. If the device has moved, anchors will be misaligned.
 To avoid this, some applications may want to reset tracking (see ARSessionRunOptions).
 @param session The session that was interrupted.
 */
optional public func sessionInterruptionEnded(_ session: ARSession)

}

@available(iOS 11.0, *)
public protocol ARSessionDelegate : ARSessionObserver {

/**
 This is called when a new frame has been updated.
 相机当前状态(ARFrame: 空间位置,图像帧等)更新
 @param session The session being run.
 @param frame The frame that has been updated.
 */
optional public func session(_ session: ARSession, didUpdate frame: ARFrame)


/**
 This is called when new anchors are added to the session.
 添加锚点
 @param session The session being run.
 @param anchors An array of added anchors.
 */
optional public func session(_ session: ARSession, didAdd anchors: [ARAnchor])


/**
 This is called when anchors are updated.
 刷新锚点
 @param session The session being run.
 @param anchors An array of updated anchors.
 */
optional public func session(_ session: ARSession, didUpdate anchors: [ARAnchor])


/**
 This is called when anchors are removed from the session.
 移除锚点
 @param session The session being run.
 @param anchors An array of removed anchors.
 */
optional public func session(_ session: ARSession, didRemove anchors: [ARAnchor])

}

</pre>

上一篇下一篇

猜你喜欢

热点阅读