Skip to content

Commit 28986d5

Browse files
committed
Update the demo, use bitmap SVG/PDF form for WebImage compatible.
Update the readme with Customization Setup chapter
1 parent c7f3df5 commit 28986d5

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

Example/SDWebImageSwiftUIDemo/AppDelegate.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
2323
SDImageCodersManager.shared.addCoder(SDImageWebPCoder.shared)
2424
SDImageCodersManager.shared.addCoder(SDImageSVGCoder.shared)
2525
SDImageCodersManager.shared.addCoder(SDImagePDFCoder.shared)
26+
// Dynamic check to support both WebImage/AnimatedImage
27+
SDWebImageManager.shared.optionsProcessor = SDWebImageOptionsProcessor { url, options, context in
28+
var context = context ?? [:]
29+
if let _ = context[.animatedImageClass] as? SDAnimatedImageProtocol {
30+
// AnimatedImage supports vector rendering
31+
} else {
32+
// WebImage supports bitmap rendering only
33+
context[.svgPrefersBitmap] = true
34+
context[.pdfPrefersBitmap] = true
35+
}
36+
return SDWebImageOptionsResult(options: options, context: context)
37+
}
2638
return true
2739
}
2840

README.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,44 @@ If you need animated image, `AnimatedImage` is the one to choose. Remember it su
164164

165165
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.
166166

167+
### Customization and configuration setup
168+
169+
This framework is based on SDWebImage, which supports advanced customization and configuration to meet different users' demand.
170+
171+
You can register multiple coder plugins for external image format. You can register multiple caches (different paths and config), multiple loaders (URLSession and Photos URLs). You can control the cache expiration date, size, download priority, etc. All in our [wiki](https://github.com/SDWebImage/SDWebImage/wiki/).
172+
173+
The best place to put these setup code for SwiftUI App, it's the `AppDelegate.swift`:
174+
175+
```swift
176+
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
177+
// Add WebP/SVG/PDF support
178+
SDImageCodersManager.shared.addCoder(SDImageWebPCoder.shared)
179+
SDImageCodersManager.shared.addCoder(SDImageSVGCoder.shared)
180+
SDImageCodersManager.shared.addCoder(SDImagePDFCoder.shared)
181+
182+
// Add default HTTP header
183+
SDWebImageDownloader.shared.setValue("image/webp,image/apng,image/*,*/*;q=0.8", forHTTPHeaderField: "Accept")
184+
185+
// Add multiple caches
186+
let cache = SDImageCache(namespace: "tiny")
187+
cache.config.maxMemoryCost = 100 * 1024 * 1024 // 100MB memory
188+
cache.config.maxDiskSize = 50 * 1024 * 1024 // 50MB disk
189+
SDImageCachesManager.shared.addCache(cache)
190+
SDWebImageManager.defaultImageCache = SDImageCachesManager.shared
191+
192+
// Add multiple loaders with Photos Asset support
193+
SDImageLoadersManager.shared.addLoader(SDWebImagePhotosLoader.shared)
194+
SDWebImageManager.defaultImageLoader = SDImageLoadersManager.shared
195+
return true
196+
}
197+
```
198+
167199
For more information, it's really recommended to check our demo, to learn detailed API usage. You can also have a check at the latest API documentation, for advanced usage.
168200

169201
## Documentation
170202

171203
+ [SDWebImageSwiftUI API documentation](https://sdwebimage.github.io/SDWebImageSwiftUI/)
172204
+ [SDWebImage API documentation](https://sdwebimage.github.io/)
173-
+ [SDWebImage Wiki](https://github.com/SDWebImage/SDWebImage/wiki/)
174205

175206
## Demo
176207

0 commit comments

Comments
 (0)