NavigationLink SwiftUI causes problem in loading images

Viewed 237

I am building an app following somewhat this tutorial, https://www.youtube.com/watch?v=Xetrbmnszjc. I decided to include API calls in it to fetch images from flickr. I have a problem, when I use an Image for Navigating to the details view, the image is not seen. If I remove the NavigationLink, it works fine.

enter image description here

enter image description here

The functionality of NavigationLink works fine though in the case when the images are not shown.

This is the code for calling view.

VStack(alignment: .leading) {
                Text(self.category.uppercased())
                    .font(.system(size: 40, weight: .heavy, design: .default))
                            .foregroundColor(.primary)
                        ScrollView(.horizontal, showsIndicators: false){
                            HStack(alignment: .top) {
                                ForEach(self.items, id: \.id){
                                    item in
                                    
                                    NavigationLink(destination: imageDetail(item:item)){
                                        imageItem(item:item)
                                            .frame(width:300)
                                            .padding(.trailing,30)
                                        }
                                }
                            }.onAppear{
                                Api().getPosts(url:"https://api.flickr.com/services/feeds/photos_public.gne?tags=\(self.category)&format=json&nojsoncallback=1",completion: {
                                    (posts) in self.items = posts
                                })
                            }
                        }
            }.padding(.leading, 20)

This is the relevant code for imageItem

 var body: some View {
        ScrollView{
            AsyncImage(url: URL(string: item.media.m.replacingOccurrences(of: "\\", with: ""))!, placeholder: Text("Loading..")
            )
            .aspectRatio(contentMode: .fill)
            .frame(width:300,height: 300)
            .cornerRadius(10)
            .shadow(radius: 10)
            
            VStack(alignment: .leading, spacing: 5){
                Text(item.title)
                    .font(.system(size: 25, weight: .bold, design: .default))
                    .foregroundColor(.primary)
                Text(item.description)
                    .font(.system(size: 20, design: .default))
                    .foregroundColor(.primary) .multilineTextAlignment(.leading)
                .lineLimit(2)
                    .frame(height: 40)
            }
        }
        
    }

AsyncImage is the class copied from https://www.vadimbulavin.com/asynchronous-swiftui-image-loading-from-url-with-combine-and-swift/

1 Answers

By default NavigationLink renders Image in template mode, so you just need to provide correct mode explicitly, like

AsyncImage(url: URL(string: item.media.m.replacingOccurrences(of: "\\", with: ""))!, placeholder: Text("Loading..")
)
.renderingMode(.original)
Related