https://github.com/eneko/swiftui-asyncimage-demo
SwiftUI AsyncImage Demo
https://github.com/eneko/swiftui-asyncimage-demo
Last synced: 3 months ago
JSON representation
SwiftUI AsyncImage Demo
- Host: GitHub
- URL: https://github.com/eneko/swiftui-asyncimage-demo
- Owner: eneko
- Created: 2021-08-02T01:29:29.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2021-08-02T01:43:54.000Z (almost 4 years ago)
- Last Synced: 2025-02-09T07:25:09.752Z (5 months ago)
- Language: Swift
- Size: 5.46 MB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# SwiftUI AsyncImage Demo
This little demo works both to demonstrate and test behavior of the
new `AsyncImage` view in SwiftUI available in Xcode 13.`AsyncImage` enables users to asynchronously download images from
the network without writing any additional code other than using
the view itself.## Features
Out of the box, it provides asynchronous downloading, placeholder
support, and, provided by `Image`, scaling and cropping.However, as seen in the recording below, `AsyncImage` does not provide
any support for caching downloaded images. These means that, as of
Xcode 13 beta 4, images will repeatedly be downloaded from the network
each time they are presented in the UI.
Note: Xcode 13 beta 4 fixes an issue where images would be downloaded
each time they would appear into view when scrolling through a `List`.## Usage Examples
`AsyncImage` is really easy to use, as shown in the examples below.
### List Example
```swift
struct ContentView: View {
@State var modal: Bool = false
let imageURL = URL(string: "https://ukmadcat.com/wp-content/uploads/2019/04/sleepy-cat.jpg")
var body: some View {
List {
Text("Hello")
ForEach(0..<10) { index in
AsyncImage(url: imageURL) { phase in
switch phase {
case .success(let image):
image
.resizable()
.scaledToFill()
default:
Color.purple.opacity(0.1)
}
}
.padding()
.onTapGesture {
modal = true
}
}
}
.sheet(isPresented: $modal) {
//
} content: {
ModalView(imageURL: imageURL)
}}
}
```### Fullscreen Image Example
```swift
struct ModalView: View {
let imageURL: URL?
var body: some View {
AsyncImage(url: imageURL) { phase in
switch phase {
case .success(let image):
image
.resizable()
.scaledToFill()
default:
Color.purple.opacity(0.1)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.edgesIgnoringSafeArea(.all)
}
}
```