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 {
            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 = ""
        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)
           = 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
        "", 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: "")) { phase in
    if let image = phase.image {
        image // Displays the loaded image.
    } else if phase.error != nil { // Indicates an error.
    } else { // Acts as a placeholder.


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