NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

SOLVED: Value of type 'AsyncImagePhase' has no member 'resizable'

Forums > SwiftUI

I have been looking at getting JSON's from an api and making them "Look Pretty" but I get this error when I try to make the image resizable and scaledToFit. Both resizable and scaledToFit get the error if you put them in seperately.

Content View:

import SwiftUI

struct ContentView: View {

    @State var data = ApodData(explanation: "", hdurl: "", title: "", date: "")

    var body: some View {
        NavigationView{
        ZStack{
            LinearGradient(colors: [.gray, .black], startPoint: .topLeading, endPoint: .bottomTrailing)
                .ignoresSafeArea()

            ScrollView{
        VStack{
            AsyncImage(url: URL(string: data.hdurl)) { image in
                image
                    .resizable()      // Error here
            }
            .frame(width: 400, height: 400)
            Text("\(data.explanation)")
                .foregroundColor(.white)
            .padding()
        }
    }
        }
        .onAppear{
            self.getData()
        }
        .navigationTitle("\(data.date)")
    }
    }

    func getData() {
        let urlString = "https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY"
        let url = URL(string: urlString)

        URLSession.shared.dataTask(with: url!) {data, _, error in
            DispatchQueue.main.async {
            if let data = data {
                do {
                    let decoder = JSONDecoder()
                    let decodedData = try decoder.decode(ApodData.self, from: data)
                    self.data = decodedData
                } catch {
                    print("Error! Something went wrong")
                }
            }

        }
        }.resume()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView(data: ApodData.init(explanation: "bdsbwhdhwsdsabhdbfbfb", hdurl:  // Creative Placeholders
        "https://apod.nasa.gov/apod/image/2202/archives_raquarii.jpg", title: "" ,date: ""))
    }
}

   

You are using this initializer on AsyncImage:

init(url: URL?, scale: CGFloat = 1, transaction: Transaction = Transaction(), content: @escaping (AsyncImagePhase) -> Content)

You should be using this initializer instead:

init<I, P>(url: URL?, scale: CGFloat = 1, content: @escaping (Image) -> I, placeholder: @escaping () -> P) where Content == _ConditionalContent<I, P>, I : View, P : View

The difference being that the second one has a placeholder parameter and passes an Image to its content closure.

   

@roosterboy

Isn't that what I have done here:

https://imgur.com/a/OU2ocoG

Also how would I change to be like the second initialiser

third one looks simpler

   

To change to the second initializer, you would need to add a placeholder closure:

AsyncImage(url: URL(string: data.hdurl)) { image in
    image
        .resizable()      // Error here
} placeholder: {
    //put your placeholder here
}

Or...

You can do something this using the initializer you originally used (example taken from the docs):

AsyncImage(url: URL(string: "https://example.com/icon.png")) { phase in
    if let image = phase.image {
        image // Displays the loaded image.
    } else if phase.error != nil {
        Color.red // Indicates an error.
    } else {
        Color.blue // Acts as a placeholder.
    }
}

1      

@roosterboy Thankyou!

   

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.