1

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

  @IBAction func captureImage(_ sender: Any) {


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

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

4

1 回答 1

5

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().

希望对你有帮助 :)

于 2018-06-20T12:12:58.350 回答