gpt4 book ai didi

swift - 获取 ARKit View 的屏幕截图

转载 作者:行者123 更新时间:2023-11-28 06:05:31 27 4
gpt4 key购买 nike

我如何插入一个按钮并使用它来拍摄照片并将其放入照片库中。我注意到在使用 arkit 时我无法拖动按钮并将它们放在 View 上。我在网上看到有人说你用 snapshot() 来拍照。

import UIKit
import SceneKit
import ARKit

class ViewController: UIViewController, ARSCNViewDelegate {

@IBOutlet var sceneView: ARSCNView!

override func viewDidLoad() {
super.viewDidLoad()

// Set the view's delegate
sceneView.delegate = self

// Show statistics such as fps and timing information
sceneView.showsStatistics = true

// Create a new scene
let scene = SCNScene(named: "art.scnassets/ship.scn")!

// Set the scene to the view
sceneView.scene = scene
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

// Create a session configuration
let configuration = ARWorldTrackingConfiguration()

// Run the view's session
sceneView.session.run(configuration)
}

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

// Pause the view's session
sceneView.session.pause()
}

func session(_ session: ARSession, didFailWithError error: Error) {
// Present an error message to the user
}

func sessionWasInterrupted(_ session: ARSession) {
// Inform the user that the session has been interrupted, for example, by presenting an overlay
}

func sessionInterruptionEnded(_ session: ARSession) {
// Reset tracking and/or remove existing anchors if consistent tracking is required
}
}

最佳答案

我做了一个简单的演示来向您展示如何组合snapshot()ARSCNViewUIBUtton。所以你可以这样定义你的 Storyboard:

enter image description here

如你所见,按钮在主视图内,但在 ARKit View 之外和上方

enter image description here

那么你的 ViewController 可能是这样的:

import UIKit
import ARKit

class ViewController: UIViewController {
@IBOutlet var arkitView:ARSCNView!
@IBOutlet var outputImageView:UIImageView!

override func viewDidLoad() {
super.viewDidLoad()
arkitView.session.run(ARWorldTrackingConfiguration())
}

@IBAction func takeScreenshotAction() {
outputImageView.image = arkitView.snapshot()
}
}

最终结果是:

enter image description here

关于swift - 获取 ARKit View 的屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48371108/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com