What is Nostr?
Josh Brown /
npub1yl8…vz34
2024-08-23 14:40:41

Josh Brown on Nostr: On Nos, we’re working on making horizontal images display in a certain aspect ...

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:

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

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
Author Public Key
npub1yl8jc6znttslcpj3p6p8vuq98awu6w0xh4lqtu0lkjr772kpx4ysfqvz34