From 7373b50154a715bb95b243428f83a6892787a819 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Tue, 24 Mar 2020 17:28:56 +0800 Subject: [PATCH] Update the readme about the imageManager usage for custom view type --- README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/README.md b/README.md index 1d7ed5c..aedce94 100644 --- a/README.md +++ b/README.md @@ -206,6 +206,38 @@ If you need powerful animated image, `AnimatedImage` is the one to choose. Remem 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. +### Use `ImageManager` for your own View type + +The `ImageManager` is a class which conforms to Combine's `ObservableObject` protocol. Which is the core fetching data source of `WebImage` we provided. + +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. + +```swift +struct MyView : View { + @ObservedObject var imageManager: ImageManager + var body: some View { + // Your custom complicated view graph + Group { + if imageManager.image != nil { + Image(uiImage: imageManager.image!) + } else { + Rectangle().fill(Color.gray) + } + } + // Trigger image loading when appear + .onAppear { self.imageManager.load() } + // Cancel image loading when disappear + .onDisappear { self.imageManager.cancel() } + } +} + +struct MyView_Previews: PreviewProvider { + static var previews: some View { + MyView(imageManager: ImageManager(url: URL(string: "https://via.placeholder.com/200x200.jpg")) + } +} +``` + ### Customization and configuration setup This framework is based on SDWebImage, which supports advanced customization and configuration to meet different users' demand.