forked from V8tr/AsyncImage
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
25 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,44 @@ | ||
## Article related to this project | ||
|
||
- [Asynchronous Image Loading from URL in SwiftUI](https://www.vadimbulavin.com/asynchronous-swiftui-image-loading-from-url-with-combine-and-swift/). | ||
|
||
--- | ||
|
||
# CachedImage | ||
|
||
The project demonstrates how to load images asynchronously in SwiftUI. | ||
`CachedImage` is a SwiftUI component to asynchronously load and display an image provided by its URL. | ||
|
||
This package is based on the [AsyncImage](https://github.com/V8tr/AsyncImage) project published by [Vadim Bulavin](https://github.com/V8tr) and described in the [Asynchronous Image Loading from URL in SwiftUI](https://www.vadimbulavin.com/asynchronous-swiftui-image-loading-from-url-with-combine-and-swift/) article. | ||
|
||
Usage: | ||
## Usage | ||
|
||
```swift | ||
// Image URLs to load | ||
let posters = [ | ||
"https://image.tmdb.org/t/p/original/pThyQovXQrw2m0s9x82twj48Jq4.jpg", | ||
"https://image.tmdb.org/t/p/original/vqzNJRH4YyquRiWxCCOH0aXggHI.jpg", | ||
"https://image.tmdb.org/t/p/original/6ApDtO7xaWAfPqfi2IARXIzj8QS.jpg", | ||
"https://image.tmdb.org/t/p/original/7GsM4mtM0worCtIVeiQt28HieeN.jpg" | ||
"https://picsum.photos/800/600?random=1", | ||
"https://picsum.photos/600/800?random=2", | ||
"https://picsum.photos/800/600?random=3", | ||
"https://picsum.photos/600/800?random=4" | ||
].map { URL(string: $0)! } | ||
|
||
struct ContentView: View { | ||
var body: some View { | ||
List(posters, id: \.self) { url in | ||
CachedImage( | ||
url: url, | ||
placeholder: { Text("Loading ...") }, | ||
image: { Image(platformImage: $0).resizable() } | ||
) | ||
.frame(idealHeight: UIScreen.main.bounds.width / 2 * 3) // 2:3 aspect ratio | ||
} | ||
GeometryReader { geometry in | ||
List(posters, id: \.self) { url in | ||
CachedImage( | ||
url: url, | ||
placeholder: { Text("Loading ...") }, | ||
image: { Image(platformImage: $0).resizable() } | ||
) | ||
.frame(idealHeight: geometry.size.width / 2 * 3) // 2:3 aspect ratio | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Result: | ||
## Demo | ||
|
||
Check the demo project provided [here](./Demo/). | ||
|
||
<p align="center"> | ||
<img src="./demo.gif" alt="How to load image from URL asynchronously in SwiftUI"/> | ||
</p> | ||
|
||
## License | ||
|
||
[Unlicensed](./LICENSE.md) |