2

When using an AsyncImage inside a VStack, the image never loads. The handler is called once, and the AsyncImagePhase is .empty. Sample code:

import SwiftUI struct ContentView: View { var body: some View { VStack { AsyncImage(url: URL(string: "https://i.scdn.co/image/ab6761610000e5ebb78f77c5583ae99472dd4a49")!) { phase in switch phase { case .success(let image): image default: EmptyView() } } } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } 

If you remove the VStack, the image loads (only when you run the app though; the Simulator in the Canvas window disappears in this case.)

I assume this is a SwiftUI bug. Any known workarounds?

1
  • Well, you're force unwrapping your url, though unlikely to cause the problem. Are you actually hitting the .success case? Also, is image a type of View Commented Feb 4, 2022 at 2:14

1 Answer 1

3

try this, replacing EmptyView() : (note there is no need for the "!" for the url)

struct ContentView: View { var body: some View { VStack { AsyncImage(url: URL(string: "https://i.scdn.co/image/ab6761610000e5ebb78f77c5583ae99472dd4a49")) { phase in switch phase { case .success(let image): image default: Color.clear // <-- here } } } } } 
Sign up to request clarification or add additional context in comments.

4 Comments

That does work, but I don't understand why...random magical fix or is there a reason?
as far as I know, EmptyView represents the absence of a view, but the AsyncImage expect to show some sort of view. But then again, it's all magic to me. You could also replace the Color.clear with the more appropriate ProgressView()
But EmptyView conforms to View, so...not sure why it would cancel the load :shrug:
Interesting issue! Even if you would only use AsyncImage(url: URL(string: "https://i.scdn.co/image/ab6761610000e5ebb78f77c5583ae99472dd4a49")) without callback, the image wouldn't load on iOS 15. On iOS 16, it seems to be resolved.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.