Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

Show images in a View based on a specific JSON field

I have an app that has to show several images (in a LazyVGrid), and I want to check if a specific word is present in a specific field of a JSON file that the app loads at start. It is possible?

Waiting for help, thank you!

Here's the code for the View:

import SwiftUI
import URLImage

struct GalleryView: View {
    
    let columns = [
        GridItem(.flexible()),
        GridItem(.flexible()),
    ]
    
    @ObservedObject var dm: DownloadManager
    
    var body: some View {
        ScrollView {
            LazyVGrid(columns: columns) {
                ForEach(dm.JSON) { busso in
                    NavigationLink(
                        destination: ImageView(busso: busso)) {
                        VStack(alignment: .center) {
                            URLImage(URL(string: busso.fotoUrl) ?? furl)
                                .resizable()
                                .aspectRatio(contentMode: .fit)
                            Text(busso.titolo)
                                .font(.headline)
                        }
                    }
                }
            }
        }
        .padding(.top, 8.0)
        .navigationBarTitle("Galleria", displayMode: .inline)
    }
}

struct GalleryView_Previews: PreviewProvider {
    static var previews: some View {
        GalleryView(dm: DownloadManager())
    }
}

Here's the code of the JSON file (I want the app to check for the word "image" in the "extra1" field, if possible)

[
    {
        "id": "3",
        "titolo": "Panorama",
        "autore": "Descrizione",
        "testo": "",
        "data": "",
        "extra1": "image",
        "extra2": "",
        "creazione": "2021-01-07 11:21:53",
        "foto": "foto/WP_20161110_001.jpg",
        "fotoUrl": "http://geniuspointfrezza.altervista.org/foto/WP_20161110_001.jpg"
    },
    {
        "id": "2",
        "titolo": "Comune di Busso",
        "autore": "Contatti",
        "testo": "Indirizzo, telefono, mail, altri dati da inserire",
        "data": "",
        "extra1": "",
        "extra2": "",
        "creazione": "2021-01-01 19:33:56",
        "foto": "foto/DSCN0914.JPG",
        "fotoUrl": "http://geniuspointfrezza.altervista.org/foto/DSCN0914.JPG"
    },
    {
        "id": "1",
        "titolo": "Chiesa",
        "autore": "Luoghi da visitare",
        "testo": "Testo di prova, abbastanza lungo per verificare l'impaginazione e correggere eventuali errori.",
        "data": "",
        "extra1": "",
        "extra2": "",
        "creazione": "2021-01-01 19:32:02",
        "foto": "foto/CAM_0044.JPG",
        "fotoUrl": "http://geniuspointfrezza.altervista.org/foto/CAM_0044.JPG"
    }
]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...