Skip to content

Commit 62774b9

Browse files
committed
Update the readme about the changes to SDWebImageContextLinkMetadata
1 parent abe1290 commit 62774b9

1 file changed

Lines changed: 12 additions & 16 deletions

File tree

README.md

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,18 @@ self.imageView = [[UIImageView alloc] init];
8383
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
8484
[self.view addSubview:self.imageView];
8585
[self.imageView sd_setImageWithURL:url completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
86-
NSLog(@"%@", @"UIImageView image load success");
86+
if (image && [image.sd_extendedObject isKindOfClass:LPLinkMetadata.class]) {
87+
NSLog(@"%@", @"UIImageView metadata load success");
88+
}
8789
}];
8890
```
8991
9092
#### Load Rich Link on LPLinkView
9193
9294
Important note on `LPLinkView`: Current iOS 13.0 contains bug that `LPLinkView` may not compatible with TableView/CollectionView cell-reusing. To workaround this issue, you can choose one of these below (one is OK):
9395
94-
1. Cache the loaded `LPLinkMetadata` by yourself, always ensure the `sd_linkMetadata` is not nil (expect first request)
95-
2. Do not using cache at all. So, always pass `SDWebImageFromLoaderOnly` to load the metadata from network
96-
3. Using trick code, create `LPLinkView` with nil URL (important)
96+
1. Do not using cache at all. So, always pass `SDWebImageFromLoaderOnly` to load the metadata from network
97+
2. Using trick code, create `LPLinkView` with empty `LPLinkMetadata`, or nil URL (important).
9798
9899
+ Objective-C
99100
@@ -102,30 +103,25 @@ NSURL *url = [NSURL URLWithString:@"https://www.apple.com/iphone/"];
102103
self.linkView = [[LPLinkView alloc] initWithURL:nil];
103104
[self.view addSubview:self.linkView];
104105
[self.linkView sd_setImageWithURL:url completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
105-
NSLog(@"%@", @"LPLinkView metadata load success");
106+
if (image && [image.sd_extendedObject isKindOfClass:LPLinkMetadata.class]) {
107+
NSLog(@"%@", @"LPLinkView metadata load success");
108+
}
106109
}];
107110
```
108111

109112
#### Using LPLinkMetadata
110113

111-
Note: You can always read and write the `LPLinkMetadata` object on the associated `NSURL` object, to provide an exist metadata from your serialization solution, or update the metadata. If the provided URL have an associated metadata, we don't do extra query with [LPMetadataProvider](https://developer.apple.com/documentation/linkpresentation/lpmetadataprovider?language=objc).
114+
For some cases, if you have the pre-fetched or pre-created `LPLinkMetadata` object, you can use `SDWebImageContextLinkMetadata` context option to associate it, without extra request to the original link URL. But in most cases, you don't need so, just use the `NSURL` point to the rich link, we query the `LPLinkMetadata` with [LPMetadataProvider](https://developer.apple.com/documentation/linkpresentation/lpmetadataprovider?language=objc).
115+
116+
Remember, if you don't want the double cache (`LPLinkMetadata` archive will contains the image data as well, but not simple image URL), do not sync or cache the metadata from callback's `image.sd_extendedObject` to your own storage using `NSCoding` methods. Let the framework do this thing.
112117

113118
+ Objective-C
114119

115120
```objective-c
116121
// Decoding a metadata from your serialization solution
117122
LPLinkMetadata *metadata = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/path/to/metadata"];
118-
// Bind the associated metadata
119-
NSURL *urlWithMetadata = metadata.originalURL;
120-
urlWithMetadata.sd_linkMetadata = metadata;
121123
// Load image without query metadata again
122-
[imageView sd_setImageWithURL:urlWithMetadata];
123-
```
124-
125-
```objective-c
126-
// If URL load success, the completion block's URL also contains the metadata
127-
LPLinkMetadata *metadata = imageURL.sd_linkMetadata;
128-
NSLog(@"[title]: %@\n[url]: %@\n[image]: %@", metadata.title, metadata.URL, metadata.imageProvider);
124+
[imageView sd_setImageWithURL:metadata.originalURL placeholderImage:nil options:0 context:@{SDWebImageContextLinkMetadata : metadata}];
129125
```
130126
131127
Note: By default, if the image is cached, we do not send request to query new metadata. If you need to query the metadata as well, consider using SDWebImage's `SDWebImageRefreshCached` option. Or using `SDWebImageFromLoaderOnly` to avoid cache during query.

0 commit comments

Comments
 (0)