DEV Community

Discussion on: Take screenshots of SwiftUI Views

Collapse
 
kevingreen22 profile image
Kevin Green • Edited

This was a great tutorial. I've been looking for a while for something like this because the HWS code doesn't work in my case. However, using the Representable method you outlined here, the screenshot is taking the whole screen and including a view I don't want in the screenshot. I've tried moving the views around in their own ZStacks but still it shows up. Any thoughts are appreciated.

Collapse
 
gualtierofr profile image
Gualtiero Frigerio • Edited

Hi, in my example I was able to screenshot a part of the screen, if you see I have one screenshot without the title and one with it. If you can share a code sample maybe I can help

And by the way, at WWDC they introduced a new way to take screenshot of Views, is called ImageRenderer so if you can target iOS 16 you don't need my workaround anymore

Collapse
 
kevingreen22 profile image
Kevin Green • Edited

I tried putting the closure in multiple different ways but it always shows the whole screen and not just the view I want captured. Here is some code from my project.
(I did see that iOS 16 has some new share screen features but im targeting iOS 14+)

struct ContentView: View {
    //... some environmentObjects
    var body: some View {
        ZStack {
            canvas
                .snapshotView { snap in
                    snapshotMaker = snap
                }
            pallet
        }   
    }

    var canvas: some View {
        Canvas()
    }
}

struct Canvas: View {
    //... some environmentObjects
    var body: some View {
        Rectangle()
            .overlay(
                // Place current images on the canvas
                ForEach(images) { $0 }
            )
    }
}
Enter fullscreen mode Exit fullscreen mode

I've tried putting the canvas and pallet in their own ZStacks.
I want a snapshot of just the canvas without the pallet.

Thanks for your help. Hope your having fun at WWDC.

Thread Thread
 
gualtierofr profile image
Gualtiero Frigerio

oh I see, I've tried with a ZStack and it takes a screenshot of everything. Basically what you see on screen is what you see on the png, while you want a screenshot of a partial content of the ZStack.
I will try to see if there is a solution to that