You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
NetworkImage is a Swift package that provides image downloading, caching, and displaying for your SwiftUI apps. It leverages the foundation URLCache, providing persistent and in-memory caches.
7
+
NetworkImage is a Swift package that provides image downloading, caching, and displaying for your
8
+
SwiftUI apps. It leverages the foundation URLCache, providing persistent and in-memory caches.
8
9
9
-
You can explore all the capabilities of this package in the [companion demo project](Examples/NetworkImageDemo).
10
+
You can explore all the capabilities of this package in the
You can also provide the name of a placeholder image that the view will display while the image is loading or, as a fallback, if an error occurs or the URL is `nil`.
33
+
To manipulate the loaded image, use the `content` parameter.
NetworkImage(url: URL(string: "https://picsum.photos/id/237/300/200")) { image in
37
+
image.resizable().scaledToFill()
38
+
}
39
+
.frame(width: 150, height: 150)
40
+
.clipped()
48
41
```
49
42
50
-
It is also possible to create network images using views to compose the network image's placeholders programmatically.
43
+
The view displays a standard placeholder that fills the available space until the image loads. You
44
+
can specify a custom placeholder by using the `placeholder` parameter.
51
45
52
46
```swift
53
-
NetworkImage(url: movie.posterURL) {
54
-
ProgressView()
55
-
} fallback: {
56
-
Text(movie.title)
57
-
.padding()
47
+
NetworkImage(url: URL(string: "https://picsum.photos/id/237/300/200")) { image in
48
+
image.resizable().scaledToFill()
49
+
} placeholder: {
50
+
Color.yellow// Shown while the image is loaded or an error occurs
58
51
}
59
-
.scaledToFit()
52
+
.frame(width: 150, height: 150)
53
+
.clipped()
60
54
```
61
55
62
-
### Styling Network Images
63
-
You can customize the appearance of network images by creating styles that conform to the `NetworkImageStyle` protocol. To set a specific style for all network images within a view, use the `networkImageStyle(_:)` modifier. In the following example, a custom style adds a grayscale effect to all the network image views within the enclosing `VStack`:
56
+
It is also possible to specify a custom fallback placeholder that the view will display if there is
NetworkImage(url: URL(string: "https://picsum.photos/id/237/300/200")) { image in
61
+
image.resizable().scaledToFill()
62
+
} placeholder: {
63
+
ProgressView() // Shown while the image is loaded
64
+
} fallback: {
65
+
Image(systemName: "photo") // Shown when an error occurs or the URL is nil
83
66
}
67
+
.frame(width: 150, height: 150)
68
+
.clipped()
69
+
.background(Color.yellow)
84
70
```
85
71
86
72
### Using NetworkImageLoader
87
-
For other use cases outside the scope of SwiftUI, you can download images directly using the shared `NetworkImageLoader`. In the following example, a view controller downloads an image and applies a transformation to it:
73
+
For other use cases outside the scope of SwiftUI, you can download images directly using the
74
+
shared `NetworkImageLoader`. In the following example, a view controller downloads an image
75
+
and applies a transformation to it.
88
76
89
77
```swift
90
78
classMyViewController: UIViewController {
@@ -132,7 +120,10 @@ For other use cases outside the scope of SwiftUI, you can download images direct
132
120
```
133
121
134
122
### NetworkImage and Testing
135
-
NetworkImage is implemented with testing in mind and provides view modifiers to stub image responses and replace the scheduler that drives the view state changes. This allows you to write synchronous tests, avoiding the use of expectations or waits. The following example shows how to use this feature with Point-Free's [SnapshotTesting](https://github.com/pointfreeco/swift-snapshot-testing) library.
123
+
NetworkImage is implemented with testing in mind and provides view modifiers to stub image
124
+
responses. This allows you to write synchronous tests, avoiding the use of expectations or waits.
125
+
The following example shows how to use this feature with Point-Free's
NetworkImage performs an explicit `.default` animation when its state changes, using a [Combine Scheduler](https://developer.apple.com/documentation/combine/scheduler). You can override this scheduler and adjust the animation with the `networkImageScheduler(_:)` view modifier.
0 commit comments