Post

Replies

Boosts

Views

Activity

Reply to screenshot on iOS26
Dear Richard Thanks for your further advice. I made followings, did I code what you said? or more other effective and simplified way available? struct CaptureContainer<Content: View>: UIViewRepresentable { @Binding var hostView: UIView? let content: Content init( hostView: Binding<UIView?>, @ViewBuilder content: () -> Content ) { self._hostView = hostView self.content = content() } func makeUIView(context: Context) -> UIView { let container = UIView() container.backgroundColor = .white let hosting = UIHostingController(rootView: content) hosting.view.backgroundColor = .white hosting.view.translatesAutoresizingMaskIntoConstraints = false hosting.view.isUserInteractionEnabled = true container.addSubview(hosting.view) NSLayoutConstraint.activate([ hosting.view.topAnchor.constraint(equalTo: container.topAnchor), hosting.view.bottomAnchor.constraint(equalTo: container.bottomAnchor), hosting.view.leadingAnchor.constraint(equalTo: container.leadingAnchor), hosting.view.trailingAnchor.constraint(equalTo: container.trailingAnchor) ]) DispatchQueue.main.async { self.hostView = hosting.view } return container } func updateUIView(_ uiView: UIView, context: Context) {} } and My Home View is struct HomeView: View { @State private var captureView: UIView? var body: some View { CaptureContainer(hostView: $captureView) { VStack { HStack { ImageView() InfoView( hostView: $captureView ) } ActionView() } } .ignoresSafeArea() } } and struct InfoView:View{ @Binding var hostView: UIView? @State var uiImage: UIImage? = nil var body: some View { Button(action: { screenShot(captureView: hostView) }){ Text("Save") } } } func screenShot(captureView:UIView?){ self.uiImage = captureScreenshot(captureView: captureView) } func captureScreenshot(captureView:UIView?) -> UIImage? { guard let view = captureView else { return nil } return view.captureHierarchyImage() } extension UIView { func captureHierarchyImage() -> UIImage { let renderer = UIGraphicsImageRenderer(bounds: bounds) return renderer.image { _ in drawHierarchy(in: bounds, afterScreenUpdates: true) } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’26
Reply to screenshot on iOS26
I solved this issue using following code, thanks for your advice func captureScreenShot() -> UIImage? { guard let rootView = UIApplication.rootView() else { return nil } return rootView.captureHierarchyImage() } extension UIApplication { static func rootView() -> UIView? { guard let scene = shared.connectedScenes.first as? UIWindowScene, let window = scene.windows.first(where: { $0.isKeyWindow }) else { return nil } return window.rootViewController?.view } } extension UIView { func captureHierarchyImage() -> UIImage { let renderer = UIGraphicsImageRenderer(bounds: bounds) return renderer.image { _ in drawHierarchy(in: bounds, afterScreenUpdates: true) } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’26
Reply to screenshot on iOS26
Thanks for your quick response. My getImage(rect:) is following as extension UIView func getImage(rect: CGRect) -> UIImage { let renderer = UIGraphicsImageRenderer(bounds: rect) return renderer.image { rendererContext in layer.render(in: rendererContext.cgContext) } } } screen shot is not available today, later I will attached with some masking required. My goal is to capture screen(UIImage) as I saw after detetion results overlayed on captured image and other UIView information such as date & time and my custom information. like default screen capture pressing hardware button. Can you provide some short sample code that render the complete visible view hierarchy via drawHierarchy(in:afterScreenUpdates:) and then fetch the current currentImage via the UIGraphicsImageRendererContext.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’26
Reply to screenshot on iOS26
Code Block is as follows let scenes = UIApplication.shared.connectedScenes let windowScene = scenes.first as? UIWindowScene let window = windowScene?.windows.first self.uiImage = window?.rootViewController?.view!.getImage(rect: rect)
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’26