Skip to content

Commit 19d2d05

Browse files
committed
Update the readme about the detailed usage and demo
1 parent 68fa018 commit 19d2d05

File tree

1 file changed

+53
-6
lines changed

1 file changed

+53
-6
lines changed

README.md

+53-6
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,49 @@ SDWebImagePhotosPlugin is a plugin for [SDWebImage](https://github.com/rs/SDWebI
1313
By using this plugin, it allows you to use your familiar View Category method from SDWebImage, to load Photos image with `PHAsset` or `localIdentifier`.
1414

1515
## Usage
16-
To support Photos Library plugin. Firstly add the photos loader to image manager. You can add to the default manager using [loaders manager](https://github.com/rs/SDWebImage/wiki/Advanced-Usage#loaders-manager), or create custom manager for usage.
16+
**Important!** To use Photos Library plugin. Firstly you need to register the photos loader to image manager.
17+
18+
There are two ways to register the photos loader. One for temporarily usage (when providing URL is definitely Photos URL but not HTTP URL), and another for global support (don't need any check, support both HTTP URL as well as Photos URL).
19+
20+
#### Use custom manager (temporarily)
21+
You can create custom manager for temporary usage. When you use custom manager, be sure to specify `SDWebImageCustomManager` context option with your custom manager for View Category methods.
1722

1823
+ Objective-C
1924

2025
```objectivec
21-
// Assign loader to manager
26+
// Assign loader to custom manager
2227
SDWebImageManager *manager = [[SDWebImageManager alloc] initWithCache:SDImageCache.sharedCache loader:SDWebImagePhotosLoader.sharedLoader];
2328
```
2429
2530
+ Swift
2631
2732
```swift
28-
// Assign loader to manager
33+
// Assign loader to custom manager
2934
let manager = SDWebImageManager(cache: SDImageCache.shared, loader: SDWebImagePhotosLoader.shared)
3035
```
3136

37+
#### Use loaders manager (globally)
38+
You can replace the default manager using [loaders manager](https://github.com/rs/SDWebImage/wiki/Advanced-Usage#loaders-manager) to support both HTTP && Photos URL globally. Put these code just at the application launch time before any SDWebImage loading was triggered.
39+
40+
+ Objective-C
41+
42+
```objectivec
43+
// Supports HTTP URL as well as Photos URL globally
44+
SDImageLoadersManager.sharedManager.loaders = @[SDWebImageDownloader.sharedDownloader, SDWebImagePhotosLoader.sharedLoader];
45+
// Replace default manager's loader implementation
46+
SDWebImageManager.defaultImageLoader = SDImageLoadersManager.sharedManager;
47+
```
48+
49+
+ Swift
50+
51+
```swift
52+
// Supports HTTP URL as well as Photos URL globally
53+
SDImageLoadersManager.shared.loaders = [SDWebImageDownloader.shared, SDWebImagePhotosLoader.shared]
54+
// Replace default manager's loader implementation
55+
SDWebImageManager.defaultImageLoader = SDImageLoadersManager.shared
56+
```
57+
58+
#### Load Images
3259
To start load Photos Library image, use the `NSURL+SDWebImagePhotosPlugin` to create a Photos URL and call View Category method.
3360

3461
+ Objective-C
@@ -41,7 +68,7 @@ NSURL *photosURL = [NSURL sd_URLWithAsset:asset];
4168
NSString *identifier;
4269
NSURL *potosURL = [NSURL sd_URLWithAssetLocalIdentifier:identifier];
4370

44-
// Load image
71+
// Load image (assume using custom manager)
4572
[imageView sd_setImageWithURL:photosURL placeholderImage:nil context:@{SDWebImageCustomManager: manager}];
4673
```
4774
@@ -55,10 +82,14 @@ let photosURL = NSURL.sd_URL(with: asset)
5582
let identifier: String
5683
let potosURL = NSURL.sd_URL(withAssetLocalIdentifier: identifier)
5784
58-
// Load image
85+
// Load image (assume using custom manager)
5986
imageView.sd_setImage(with: photosURL, placeholderImage: nil, context: [.customManager: manager])
6087
```
6188

89+
#### Animated Images
90+
SDWebImagePhotosPlugin supports GIF images stored in Photos Library as well. Just use the same API as normal images to query the asset. We will query the image data and decode the animated images (compatible with `UIImageView` as well as [SDAnimatedImageView](https://github.com/rs/SDWebImage/wiki/Advanced-Usage#animated-image-50))
91+
92+
#### Custom Options
6293
To specify custom options like `PHFetchOptions` or `PHImageRequestOptions`. Either to change the property in loader, or provide a context options for each Photos Library image request.
6394

6495
+ Objective-C
@@ -67,6 +98,8 @@ To specify custom options like `PHFetchOptions` or `PHImageRequestOptions`. Eith
6798
// loader-level control
6899
SDWebImagePhotosLoader.sharedLoader.fetchOptions = fetchOptions;
69100
// request-level control
101+
PHImageRequestOptions *requestOptions = [PHImageRequestOptions new];
102+
requestOptions.networkAccessAllowed = YES;
70103
[imageView sd_setImageWithURL:photosURL placeholderImage:nil context:@{SDWebImageContextPhotosImageRequestOptions: requestOptions, SDWebImageCustomManager: manager}];
71104
```
72105
@@ -76,7 +109,9 @@ SDWebImagePhotosLoader.sharedLoader.fetchOptions = fetchOptions;
76109
// loader-level control
77110
SDWebImagePhotosLoader.shared.fetchOptions = fetchOptions
78111
// request-level control
79-
imageView.sd_setImage(with: photosURL, placeholderImage: nil, context:[.requestOptions: requestOptions, .customManager: manager])
112+
let requestOptions = PHImageRequestOptions()
113+
requestOptions.networkAccessAllowed = true
114+
imageView.sd_setImage(with: photosURL, placeholderImage: nil, context:[.photosImageRequestOptions: requestOptions, .customManager: manager])
80115
```
81116

82117
## Tips
@@ -112,6 +147,18 @@ Note that because the dependency SDWebImage currently is in beta. You should use
112147
github "SDWebImage/SDWebImagePhotosPlugin"
113148
```
114149

150+
## Demo
151+
152+
If you have some issue about usage, SDWebImagePhotosPlugin provide a demo for iOS && macOS platform. To run the demo, clone the repo and run the following command.
153+
154+
```bash
155+
cd Example/
156+
pod install
157+
open SDWebImagePhotosPlugin.xcworkspace
158+
```
159+
160+
After the Xcode project was opened, click `Run` to build and run the demo.
161+
115162
## Author
116163

117164
DreamPiggy, [email protected]

0 commit comments

Comments
 (0)