Swift:迄今为止的 ARKit 时间间隔

发布于 2025-01-16 07:04:57 字数 1040 浏览 1 评论 0原文

ARSessioncurrentFrame (ARFrame) 具有 TimeInterval 类型的 timestamp 属性,该属性表示捕获帧时的正常运行时间。

我需要将此 TimeInterval 转换为设备的当前时域。

如果我对时间戳的假设是正确的,那么将内核 BootTime 和时间戳相加就会得到正确的日期。

问题:将内核 BootTime 和 timestamp 添加在一起会得到一个不正确的日期。 (取决于设备的上次启动时间,最多有 2 天的差异)

当前代码:

func kernelBootTime() -> Date {
    var mib = [CTL_KERN, KERN_BOOTTIME]
    var bootTime = timeval()
    var bootTimeSize = MemoryLayout<timeval>.stride

    if sysctl(&mib, UInt32(mib.count), &bootTime, &bootTimeSize, nil, 0) != 0 {
        fatalError("Could not get boot time, errno: \(errno)")
    }

    return Date(timeIntervalSince1970: Double(bootTime.tv_sec) + Double(bootTime.tv_usec) / 1_000_000.0)
}

public func checkTime(_ session: ARSession) {
    guard let frame = session.currentFrame else { return }
    print(Date(timeInterval: frame.timestamp, since: kernelBootTime()))
}

The currentFrame (ARFrame) of ARSession has a timestamp attribute of type TimeInterval which represents the uptime at the moment the frame has been captured.

I need to convert this TimeInterval to the current time domain of the device.

If my assumption about timestamp is correct, adding the kernel BootTime and timestamp together would give me the correct date.

Problem: Adding the kernel BootTime and timestamp together gives me an Date that is not correct. (depending on the device`s last boot time up to 2 days variance)

Current Code:

func kernelBootTime() -> Date {
    var mib = [CTL_KERN, KERN_BOOTTIME]
    var bootTime = timeval()
    var bootTimeSize = MemoryLayout<timeval>.stride

    if sysctl(&mib, UInt32(mib.count), &bootTime, &bootTimeSize, nil, 0) != 0 {
        fatalError("Could not get boot time, errno: \(errno)")
    }

    return Date(timeIntervalSince1970: Double(bootTime.tv_sec) + Double(bootTime.tv_usec) / 1_000_000.0)
}

public func checkTime(_ session: ARSession) {
    guard let frame = session.currentFrame else { return }
    print(Date(timeInterval: frame.timestamp, since: kernelBootTime()))
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄荷梦 2025-01-23 07:04:57

我找到了解决方案。

var refDate = Date.now - ProcessInfo.processInfo.systemUptime

为您提供上次设备重新启动的日期

public func checkTime(_ session: ARSession) {
    guard let frame = session.currentFrame else { return }
    print(Date(timeInterval: frame.timestamp, since: refDate))
}

打印拍摄图像的确切时间。 (以UTC时间表示)

I found the solution.

var refDate = Date.now - ProcessInfo.processInfo.systemUptime

gives you the date of the last device restart

public func checkTime(_ session: ARSession) {
    guard let frame = session.currentFrame else { return }
    print(Date(timeInterval: frame.timestamp, since: refDate))
}

prints the exact time when the image was taken. (in UTC time)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文