'SwiftUI set corner radius depending on its own height

When I set corner radius with constant value, depending on the size of the image, the results don't come out the way I want.

Image(“myImage”)
    .cornerRadius(25)

What I want in app. enter image description here More clipped in Widget enter image description here So I want to set radius value like image.size.height / 5, depending on its own size.

When I think about overlay, code will be like below,

Image("myImage")
.overlay(
    GeometryReader { geo in
        RoundedRectangle(cornerRadius: geo.size.height / 5)
    }
)

but when it comes to clipShape to apply corner radius, I don't know how to do.



Solution 1:[1]

Try using clip shape with Circle as shape - Using it with an aspect ratio will give you a perfectly square image and content mode will make sure the image fits perfectly.

Use something like this -

Image(“myImage”)
    .aspectRatio(1, contentMode: .fit)
    .clipShape(Circle())

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pankaj Gaikar