I am trying to take a screenshot of my swift ui view and put it aside as a UIImage. The problem I’ve had is that it solely takes a screenshot of the highest layer (I’ve a video view from the again digital camera with an overlay over it, it solely saves a picture of that overlay).
personal func captureScreenshot(of viewsToCapture: [UIView]) -> UIImage? {
// Calculate the mixed bounds of all views
var combinedFrame = CGRect.zero
for view in viewsToCapture {
combinedFrame = combinedFrame.union(view.body)
}
// Create a brand new context for the mixed measurement
UIGraphicsBeginImageContextWithOptions(combinedFrame.measurement, false, 0.0)
// Iterate over views and seize their content material
for view in viewsToCapture {
// Offset the view's body to its place within the mixed picture
let offsetFrame = view.body.offsetBy(dx: -combinedFrame.origin.x, dy: -combinedFrame.origin.y)
view.drawHierarchy(in: offsetFrame, afterScreenUpdates: true)
}
// Get the mixed picture
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return screenshot
}
That is the code I am utilizing
I attempted altering the construction I take advantage of to take the picture to mix a screenshot of the video view with the highest view, however my app crashed each time I attempted that.