Image not found in asset catalog - SwiftUI

63 views Asked by At

I have some images on the asset catalog, all of them working fine, but one in particular is giving me trouble.

I have a set of images called "a1", "a2", "a3" and "a4" in the asset catalog. There is a view that shows one of those images depending on a certain value. The images are shown with the following code:

struct GameTable: View {

...

var body: some View {

...

    Image("a\(game.rolls[idx].act_value)")
        .resizable()
        .frame(width: 20, height: 20)

...

}
}

The value of game.rolls[idx].act_value is of course in between 1 and 4.

This was all working fine, until I added the possibility of exporting the view that contains those images as a PDF. This is the code that does that:

struct GameDetailView: View {

...

var body: some View {

...

    ScrollView {
        GameTable(game: game)
            .padding()
            .navigationTitle("tabla \(game.name)")
            .toolbar {
                ToolbarItem(placement: .topBarTrailing) {
                    ShareLink(item: tablePDF())
                }
            }
    }

...

}

@MainActor func tablePDF() -> URL {
    let renderer = ImageRenderer(content: GameTable(game: game).frame(width: 393).padding())
    let url = URL.documentsDirectory.appending(path: "tabla_\(game.name).pdf")
    renderer.render { size, context in
        var pdfDimension = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        guard let pdf = CGContext(url as CFURL, mediaBox: &pdfDimension, nil) else {
            return
        }
        pdf.beginPDFPage(nil)
        context(pdf)
        pdf.endPDFPage()
        pdf.closePDF()
    }
    return url
}
}

The view still shows the image "a2" as expected, but the following error is thrown and the exported PDF does not show it:

No image named 'a2' found in asset catalog for /private/var/containers/Bundle/Application/A027DBCC-0DED-4CF7-8782-A48E52AC1866/dados catan.app

The weird thing is that this is only happening with "a2", while the other three images work perfectly fine.

0

There are 0 answers