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 {
            LinearGradient(colors: [.gray, .black], startPoint: .topLeading, endPoint: .bottomTrailing)

            AsyncImage(url: URL(string: data.hdurl)) { image in
                    .resizable()      // Error here
            .frame(width: 400, height: 400)

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


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.



Isn't that what I have done here:


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
        .resizable()      // Error here
} placeholder: {
    //put your placeholder here


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.


@roosterboy Thankyou!


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.