ARKit 显示损坏的 UIWebView

Posted

技术标签:

【中文标题】ARKit 显示损坏的 UIWebView【英文标题】:ARKit Showing broken UIWebView 【发布时间】:2019-10-09 15:31:46 【问题描述】:

我正在尝试使用 ARImageTrackingConfiguration 检测图像,当检测到图像时,会显示 UIWebView 而不是扫描的图像。我阅读了很多关于新 WBWebView 的问题,但也无法正常工作。

问题似乎是 UIWebView 没有从主线程更新,即使我正在使用 DispatchMain。

关于一个相关主题(我使用的代码几乎相同),如果我尝试放置一个 SKVideoNode 而不是 UIWebView,如果我设置 plane.cornerRadius = 0.25

代码如下


func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) 
        guard let imageAnchor = anchor as? ARImageAnchor else  return 

        updateQueue.async 
            let physicalWidth = imageAnchor.referenceImage.physicalSize.width
            let physicalHeight = imageAnchor.referenceImage.physicalSize.height

            // Create a plane geometry to visualize the initial position of the detected image
            let mainPlane = SCNPlane(width: physicalWidth, height: physicalHeight)

            mainPlane.firstMaterial?.colorBufferWriteMask = .alpha

            // Create a SceneKit root node with the plane geometry to attach to the scene graph
            // This node will hold the virtual UI in place
            let mainNode = SCNNode(geometry: mainPlane)
            mainNode.eulerAngles.x = -.pi / 2
            mainNode.renderingOrder = -1
            mainNode.opacity = 1

            // Add the plane visualization to the scene
            node.addChildNode(mainNode)

            // Perform a quick animation to visualize the plane on which the image was detected.
            // We want to let our users know that the app is responding to the tracked image.
            self.highlightDetection(on: mainNode, width: physicalWidth, height: physicalHeight, completionHandler: 

            DispatchQueue.main.async 
            let request = URLRequest(url: URL(string: "https://www.facebook.com/")!)
            let webView = UIWebView(frame: CGRect(x: 0, y: 0, width: 400, height: 672))
            webView.loadRequest(request)

            let webViewPlane = SCNPlane(width: xOffset, height: xOffset * 1.4)
            webViewPlane.cornerRadius = 0.25

            let webViewNode = SCNNode(geometry: webViewPlane)
            webViewNode.geometry?.firstMaterial?.diffuse.contents = webView
            webViewNode.position.z -= 0.5
            webViewNode.opacity = 0

            rootNode.addChildNode(webViewNode)
            webViewNode.runAction(.sequence([
                .wait(duration: 3.0),
                .fadeOpacity(to: 1.0, duration: 1.5),
                .moveBy(x: xOffset * 1.1, y: 0, z: -0.05, duration: 1.5),
                .moveBy(x: 0, y: 0, z: -0.05, duration: 0.2)
                ])
            )
        


            )
        
    


VIDEO VERSION

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) 
        guard let imageAnchor = anchor as? ARImageAnchor else  return 

        updateQueue.async 
            let physicalWidth = imageAnchor.referenceImage.physicalSize.width
            let physicalHeight = imageAnchor.referenceImage.physicalSize.height

            // Create a plane geometry to visualize the initial position of the detected image
            let mainPlane = SCNPlane(width: physicalWidth, height: physicalHeight)

            mainPlane.firstMaterial?.colorBufferWriteMask = .alpha

            // Create a SceneKit root node with the plane geometry to attach to the scene graph
            // This node will hold the virtual UI in place
            let mainNode = SCNNode(geometry: mainPlane)
            mainNode.eulerAngles.x = -.pi / 2
            mainNode.renderingOrder = -1
            mainNode.opacity = 1

            // Add the plane visualization to the scene
            node.addChildNode(mainNode)

            // Perform a quick animation to visualize the plane on which the image was detected.
            // We want to let our users know that the app is responding to the tracked image.
            self.highlightDetection(on: mainNode, width: physicalWidth, height: physicalHeight, completionHandler: 

                let size = imageAnchor.referenceImage.physicalSize

                var videoNode = SKVideoNode()

                switch imageAnchor.name 
                case "Elephant-PostCard":
                    videoNode = SKVideoNode(fileNamed: "Movies/cat.mp4")

                case "puppy":
                    videoNode = SKVideoNode(fileNamed: "puppy.mov")
                default:
                    break
                
                // invert our video so it does not look upside down
                videoNode.yScale = -1.0

                videoNode.play()

                let videoScene = SKScene(size: CGSize(width: 1280, height: 720))

                videoScene.anchorPoint = CGPoint(x: 0.5, y: 0.5)
                videoScene.addChild(videoNode)

                let plane = SCNPlane(width: size.width, height: size.height)
                plane.cornerRadius = 0.25
                plane.firstMaterial?.diffuse.contents = videoScene

                let planeNode = SCNNode(geometry: plane)
                plane.firstMaterial?.isDoubleSided = true

                mainNode.addChildNode(planeNode)

            )
        
    



【问题讨论】:

避免使用UIWebView。它已被弃用。使用WKWebView 嗨,正如我在帖子中提到的那样,我已经尝试过使用 WKWebView,但我只是无法正常工作......而且从我在 ***.com 中发现的内容来看,许多用户报告说使用 ARKit,WKWebView 没有不行,最好用Uiwebview 【参考方案1】:

UIWebView 来自 UIKit 模块现已弃用。

改用来自WebKit 模块的WKWebView。

【讨论】:

以上是关于ARKit 显示损坏的 UIWebView的主要内容,如果未能解决你的问题,请参考以下文章

存储 ARKit 点云数据并检索显示

在 ARKit 中同时显示相机捕获图像和场景背景

在 ARKit 上显示具有透明背景的视频

ARKit 中的 FaceTracking – 如何在屏幕上显示“lookAtPoint”

ARKit 在屏幕底部/底部显示地图

如何使用 ARKit 检测触摸并显示新的 SCNPlane?