On Nos, we’re working on…

On Nos, we’re working on making horizontal images display in a certain aspect ratio, like 4:3. We want the image to fill the space available and then be cropped as needed (assuming the original image isn’t 4:3). I couldn’t believe how hard this was to do in SwiftUI, so if you’re doing something like this, here’s what I found after too many hours of reading and experimenting: https://stackoverflow.com/a/78903559/2030

TLDR? Here’s an example:

<br>Image("example")<br> .resizable()<br> .aspectRatio(contentMode: .fill)<br> .frame(<br> minWidth: 0,<br> maxWidth: .infinity,<br> minHeight: 0,<br> maxHeight: .infinity<br> )<br> .aspectRatio(4 / 3, contentMode: .fit)<br> .clipShape(.rect)<br>

Want to see it in the Nos codebase? That’s here: https://github.com/planetary-social/nos/blob/main/Nos/Views/Components/Media/ImageButton.swift#L16