swift 中 ARKit 中的屏幕截图问题

ScreenShot issue in ARKit in swift

我有使用 ARSCNView 的应用程序。我正在尝试通过单击按钮截取屏幕截图并将该图像保存在图库中。但是当我截取屏幕截图时,它不会显示该屏幕上的内容。只显示该图像,我在上面有一些标签,但它没有在图像中显示。这是我的代码,

  @IBAction func captureImage(_ sender: Any) {


 image = sceneView.snapshot()
     UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil)
}

如何在屏幕截图中显示 ARSCView 上的标签和按钮?

snapshot()只会截取场景。

要截取带有标签和按钮的场景,请使用以下方法:

 func snapshot(of rect: CGRect? = nil) -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.isOpaque, 0)
        self.view.drawHierarchy(in: self.view.bounds, afterScreenUpdates: true)
        let fullImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        guard let image = fullImage, let rect = rect else { return fullImage }
        let scale = image.scale
        let scaledRect = CGRect(x: rect.origin.x * scale, y: rect.origin.y * scale, width: rect.size.width * scale, height: rect.size.height * scale)
        guard let cgImage = image.cgImage?.cropping(to: scaledRect) else { return nil }
        return UIImage(cgImage: cgImage, scale: scale, orientation: .up)
    } 

截图:

 @IBAction func takeScreenShotTapped(_ sender: Any) {
    let screenShot = snapshot(of: CGRect(x: 80, y: 80, width: 100, height: 100))
}

如果你想截取全屏屏幕,那么只需调用snapshot()

希望这对您有所帮助:)