AsyncImage - SwiftUI

 

AsyncImage is a built-in SwiftUI view introduced in iOS 15+ to asynchronously load and display images from a URL

It provides a simple way to load images without writing URLSession code to download data in background and set it to ImageView on Main thread.


import SwiftUI


struct AsyncImageView: View {

    var images: [URL] = [

        URL(string: "https://picsum.photos/id/1000/400/400")!,

        URL(string: "https://picsum.photos/id/1001/400/400")!,

        URL(string: "https://picsum.photos/id/1002/400/400")!,

        URL(string: "https://picsum.photos/id/1003/400/400")!,

        URL(string: "https://picsum.photos/id/1004/400/400")!,

        URL(string: "https://picsum.photos/id/1005/400/400")!,

        URL(string: "https://picsum.photos/id/1006/400/400")!,

        URL(string: "https://picsum.photos/id/1007/400/400")!,

        URL(string: "https://picsum.photos/id/1008/400/400")!,

        URL(string: "https://picsum.photos/id/1009/400/400")!

    ]

    var body: some View {

        NavigationView {

            List(0..<images.count, id: \.self) { index in

                AsyncImage(url: images[index]) { phase in

                    if let image = phase.image {

                        image

                            .resizable()

                            .aspectRatio(contentMode: .fill)

                        let _ = setStringBox()

                        let _ = setIntBox()

                    } else if phase.error != nil {

                        Text("No image available")

                    } else {

                        Image(systemName: "photo")

                    }

                }

                .frame(maxWidth: .infinity)

                .border(Color.gray)

                .padding()

            }

            .navigationTitle("Async Image SwiftUI")

        }

     }

 }



struct AsyncImageView_Previews: PreviewProvider {

    static var previews: some View {

        AsyncImageView()

    }

}











Comments

Popular posts from this blog

payUmoney Payment Gateway Integration in iOS App (Objective C)

UITableView - Add or Remove Rows Dynamically (Objective C & Swift Source codes)

Unique Identifier UUID - iOS