How define a size for image with AsyncImage

173 Views Asked by At

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.

1

There are 1 best solutions below

1
Johnatan Kayevo On

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