4

The problem: When i use AsyncImage to display an image. The size of image is big and dont fit the size of frame.

Thats the code i used:

AsyncImage(url: URL(string: mockImageUrl))
        .frame(width: 200, height: 300)

And this is how image is displayed:

image on emulator

What I need: I need to do image fits the frame.

What I tryed: I defined the size of AsyncImage but the image still doesnt feat the height and width of frame.

2

1 Answer 1

19

Solution:

To make image follow the size of AsyncImage you need change the scale of image like this:

AsyncImage(url: URL(string: mockImageUrl)){ result in
            result.image?
                .resizable()
                .scaledToFill()
        }
        .frame(width: 200, height: 200)

".resizable()" code allow the image size fit the frame of AsyncImage.

image on emulator

Documentation examples:

https://developer.apple.com/documentation/swiftui/asyncimage

Sign up to request clarification or add additional context in comments.

1 Comment

yes, just like I said in my comment, see the example given by Apple

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.