Skip to content

Commit 7373b50

Browse files
committed
Update the readme about the imageManager usage for custom view type
1 parent 88e4dea commit 7373b50

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,38 @@ If you need powerful animated image, `AnimatedImage` is the one to choose. Remem
206206

207207
But, because `AnimatedImage` use `UIViewRepresentable` and driven by UIKit, currently there may be some small incompatible issues between UIKit and SwiftUI layout and animation system, or bugs related to SwiftUI itself. We try our best to match SwiftUI behavior, and provide the same API as `WebImage`, which make it easy to switch between these two types if needed.
208208

209+
### Use `ImageManager` for your own View type
210+
211+
The `ImageManager` is a class which conforms to Combine's `ObservableObject` protocol. Which is the core fetching data source of `WebImage` we provided.
212+
213+
For advanced use case, like loading image into the complicated View graph which you don't want to use `WebImage`. You can directly bind your own View type with the Manager, which provide the Source of Truth of loading images.
214+
215+
```swift
216+
struct MyView : View {
217+
@ObservedObject var imageManager: ImageManager
218+
var body: some View {
219+
// Your custom complicated view graph
220+
Group {
221+
if imageManager.image != nil {
222+
Image(uiImage: imageManager.image!)
223+
} else {
224+
Rectangle().fill(Color.gray)
225+
}
226+
}
227+
// Trigger image loading when appear
228+
.onAppear { self.imageManager.load() }
229+
// Cancel image loading when disappear
230+
.onDisappear { self.imageManager.cancel() }
231+
}
232+
}
233+
234+
struct MyView_Previews: PreviewProvider {
235+
static var previews: some View {
236+
MyView(imageManager: ImageManager(url: URL(string: "https://via.placeholder.com/200x200.jpg"))
237+
}
238+
}
239+
```
240+
209241
### Customization and configuration setup
210242

211243
This framework is based on SDWebImage, which supports advanced customization and configuration to meet different users' demand.

0 commit comments

Comments
 (0)