UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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: ""))
    }
}

2      

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.

2      

@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

2      

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.
    }
}

3      

@roosterboy Thankyou!

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.