Skip to content

Commit 5938dd0

Browse files
committed
Update the readme about the external loader/caches/coders usage tutorial
1 parent cd8625b commit 5938dd0

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

README.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,101 @@ NavigationView {
365365
}
366366
```
367367

368+
369+
#### Using with external loaders/caches/coders
370+
371+
SDWebImage itself, supports many custom loaders (like [Firebase Storage](https://github.com/firebase/FirebaseUI-iOS) and [PhotosKit](https://github.com/SDWebImage/SDWebImagePhotosPlugin)), caches (like [YYCache](https://github.com/SDWebImage/SDWebImageYYPlugin) and [PINCache](https://github.com/SDWebImage/SDWebImagePINPlugin)), and coders (like [WebP](https://github.com/SDWebImage/SDWebImageWebPCoder) and [AVIF](https://github.com/SDWebImage/SDWebImageAVIFCoder), even [Lottie](https://github.com/SDWebImage/SDWebImageLottieCoder)).
372+
373+
Here is the tutorial to setup these external components with SwiftUI environment.
374+
375+
##### Setup external SDKs
376+
377+
You can put the setup code inside your SwiftUI `App.init()` method.
378+
379+
```swift
380+
@main
381+
struct MyApp: App {
382+
383+
init() {
384+
// Custom Firebase Storage Loader
385+
FirebaseApp.configure()
386+
SDImageLoadersManager.shared.loaders = [FirebaseUI.StorageImageLoader.shared]
387+
SDWebImageManager.defaultImageLoader = SDImageLoadersManager.shared
388+
// WebP support
389+
SDImageCodersManager.shared.addCoder(SDImageWebPCoder.shared)
390+
}
391+
392+
var body: some Scene {
393+
WindowGroup {
394+
ContentView()
395+
}
396+
}
397+
}
398+
```
399+
400+
or, if your App have complicated `AppDelegate` class, put setup code there:
401+
402+
```swift
403+
class AppDelegate: NSObject, UIApplicationDelegate {
404+
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
405+
SDImageCachesManager.shared.caches = [YYCache(name: "default")]
406+
SDWebImageManager.defaultImageCache = SDImageCachesManager.shared
407+
return true
408+
}
409+
}
410+
411+
@main
412+
struct MyApp: App {
413+
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
414+
415+
var body: some Scene {
416+
WindowGroup {
417+
ContentView()
418+
}
419+
}
420+
}
421+
```
422+
423+
##### Use external SDKs
424+
425+
For some of custom loaders, you need to create the `URL` struct with some special APIs, so that SDWebImage can retrieve the context from other SDKs, like:
426+
427+
+ FirebaseStorage
428+
429+
```swift
430+
let storageRef: StorageReference
431+
let storageURL = NSURL.sd_URL(with: storageRef) as URL?
432+
// Or via convenience extension
433+
let storageURL = storageRef.sd_URLRepresentation
434+
```
435+
436+
+ PhotosKit
437+
438+
```swift
439+
let asset: PHAsset
440+
let photosURL = NSURL.sd_URL(with: asset) as URL?
441+
// Or via convenience extension
442+
let photosURL = asset.sd_URLRepresentation
443+
```
444+
445+
For some of custom coders, you need to request the image with some options to control the behavior, like Vector Images SVG/PDF. Because SwiftUI.Image or WebImage does not supports vector graph at all.
446+
447+
+ SVG/PDF Coder
448+
449+
```swift
450+
let vectorURL: URL? // URL to SVG or PDF
451+
WebImage(url: vectorURL, context: [.imageThumbnailPixelSize: CGSize(width: 100, height: 100)])
452+
```
453+
454+
+ Lottie Coder
455+
456+
```swift
457+
let lottieURL: URL? // URL to Lottie.json
458+
WebImage(url: lottieURL, isAnimating: $isAnimating)
459+
```
460+
461+
For caches, you actually don't need to worry about anything. It just works after setup.
462+
368463
#### Using for backward deployment and weak linking SwiftUI
369464

370465
SDWebImageSwiftUI supports to use when your App Target has a deployment target version less than iOS 13/macOS 10.15/tvOS 13/watchOS 6. Which will weak linking of SwiftUI(Combine) to allows writing code with available check at runtime.

0 commit comments

Comments
 (0)