根据特定的 JSON 字段在视图中显示图像

时间:2021-01-07 10:29:28

标签: json image swiftui field

我有一个应用程序,它必须显示多个图像(在 LazyVGrid 中),我想检查应用程序在启动时加载的 JSON 文件的特定字段中是否存在特定单词。有可能吗?

等待帮助,谢谢!

这是视图的代码:

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())
    }
}

这是 JSON 文件的代码(如果可能,我希望应用程序检查“extra1”字段中的“图像”一词)

[
    {
        "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"
    }
]

0 个答案:

没有答案