Skip to content

Commit 505fd61

Browse files
committed
Update the screenshot and readme
1 parent 9e17873 commit 505fd61

4 files changed

Lines changed: 53 additions & 19 deletions

File tree

Example/Screenshot/LinkDemo.png

-316 KB
Loading

README.md

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ SDWebImageLinkPlugin is a plugin for [SDWebImage](https://github.com/rs/SDWebIma
1313

1414
By using this plugin, it allows you to use your familiar View Category method from SDWebImage, to load rich link's poster image, with the URL or `LPMetadata`. And make it easy to use `LPLinkView` with cache support.
1515

16+
See more about Link Presentation in [WWDC 262: Embedding and Sharing Visually Rich Links](https://developer.apple.com/videos/play/wwdc2019/262/)
17+
1618
## Requirements
1719

1820
+ iOS 13+
@@ -59,42 +61,75 @@ To use the LinkPlugin, you should setup the loader firstly. See more here in [Wi
5961
+ Objective-C
6062

6163
```objective-c
62-
[SDImageLoadersManager.sharedManager addLoader:SDImageLinkLoader.sharedLoader];
63-
SDWebImageManager.defaultImageLoader = SDImageLoadersManager.sharedManager;
64+
// Put this code on AppDelegate.m
65+
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
66+
{
67+
// Override point for customization after application launch.
68+
[SDImageLoadersManager.sharedManager addLoader:SDImageLinkLoader.sharedLoader];
69+
SDWebImageManager.defaultImageLoader = SDImageLoadersManager.sharedManager;
70+
return YES;
71+
}
6472
```
6573

66-
#### Load Rich Link on UIImageView/LPLinkView
74+
#### Load Rich Link on UIImageView
6775

68-
The simple and fast usage, it to use the LinkPlugin provided category on `UIImageView` and `LPLinkView`.
76+
The simple and fast usage, it to use the SDWebImage provided category on `UIImageView`.
6977

7078
+ Objective-C
7179

72-
```objectivec
73-
NSURL *url1 = [NSURL URLWithString:@"https://www.apple.com/iphone/"];
74-
NSURL *url2 = [NSURL URLWithString:@"https://webkit.org/"];
75-
self.linkView = [[LPLinkView alloc] initWithURL:url1];
80+
```objective-c
81+
NSURL *url = [NSURL URLWithString:@"https://webkit.org/"];
7682
self.imageView = [[UIImageView alloc] init];
7783
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
78-
[self.view addSubview:self.linkView];
7984
[self.view addSubview:self.imageView];
85+
[self.imageView sd_setImageWithURL:url completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
86+
NSLog(@"%@", @"UIImageView image load success");
87+
}];
88+
```
89+
90+
#### Load Rich Link on LPLinkView
91+
92+
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):
93+
94+
1. Cache the loaded `LPMetadata` 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)
97+
98+
+ Objective-C
8099
81-
[self.linkView sd_setImageWithURL:url1 placeholderImage:nil options:SDWebImageFromLoaderOnly completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
100+
```objectivec
101+
NSURL *url = [NSURL URLWithString:@"https://www.apple.com/iphone/"];
102+
self.linkView = [[LPLinkView alloc] initWithURL:nil];
103+
[self.view addSubview:self.linkView];
104+
[self.linkView sd_setImageWithURL:url completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
82105
NSLog(@"%@", @"LPLinkView metadata load success");
83106
}];
84-
[self.imageView sd_setImageWithURL:url2 completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
85-
NSLog(@"%@", @"UIImageView image load success");
86-
}];
87107
```
88108

109+
#### Using LPMetadata
110+
89111
Note: You can always read and write the `LPMetadata` 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).
90112

91113
+ Objective-C
92114

93115
```objective-c
116+
// Decoding a metadata from your serialization solution
117+
LPLinkMetadata *metadata = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/path/to/metadata"];
118+
// Bind the associated metadata
119+
NSURL *urlWithMetadata = metadata.originalURL;
120+
urlWithMetadata.sd_linkMetadata = metadata;
121+
// 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
94127
LPLinkMetadata *metadata = imageURL.sd_linkMetadata;
95128
NSLog(@"[title]: %@\n[url]: %@\n[image]: %@", metadata.title, metadata.URL, metadata.imageProvider);
96129
```
97130

131+
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.
132+
98133
Note: By default, we prefer to load the image only, which does not generate the image data. This can increase the loading speed. But however, you can also specify to generate the image data by using `SDWebImageContextLinkRequestImageData` context option.
99134

100135
## Demo
@@ -109,6 +144,8 @@ open SDWebImageLinkPlugin.xcworkspace
109144

110145
After the Xcode project was opened, click `Run` to build and run the demo.
111146

147+
Tips: The iOS demo provide the both of two views' usage. Click `Switch View` to toggle between UIImageView/LPLinkView.
148+
112149
## Screenshot
113150

114151
<img src="https://raw.githubusercontent.com/SDWebImage/SDWebImageLinkPlugin/master/Example/Screenshot/LinkDemo.png" width="300" />

SDWebImageLinkPlugin/Classes/LPLinkView+WebCache.m

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,10 @@ - (void)sd_setImageWithURL:(nullable NSURL *)url
7272
// LPLinkMetadata.imageProvider on iOS 13.1 contains bug which cause async query, and not compatible for cell-reusing. Radar FB7462933
7373
id<LPImageProtocol> linkImage = [[NSClassFromString(LPImageClass) alloc] initWithPlatformImage:image];
7474
[metadata setValue:linkImage forKey:@"image"];
75-
imageURL.sd_linkMetadata = metadata;
7675
} else {
7776
metadata = [[LPLinkMetadata alloc] init];
7877
metadata.originalURL = url;
7978
metadata.URL = imageURL;
80-
imageURL.sd_linkMetadata = metadata;
8179
}
8280
sself.metadata = metadata;
8381
} progress:progressBlock completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {

SDWebImageLinkPlugin/Classes/SDImageLinkLoader.m

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ - (BOOL)canRequestImageForURL:(NSURL *)url {
7777
// Let's check wether input URL already have an associated LPLinkMetadata
7878
LPLinkMetadata *metadata = url.sd_linkMetadata;
7979
if (metadata) {
80-
[self fetchImageWithMetadata:metadata operation:operation options:options context:context progress:progressBlock completed:completedBlock];
80+
[self fetchImageWithMetadata:metadata operation:operation url:url options:options context:context progress:progressBlock completed:completedBlock];
8181
} else {
8282
LPMetadataProvider *provider = [[LPMetadataProvider alloc] init];
8383
provider.timeout = self.timeout;
@@ -91,16 +91,15 @@ - (BOOL)canRequestImageForURL:(NSURL *)url {
9191
}
9292
// Associated the link metadata to URL, weak reference
9393
url.sd_linkMetadata = metadata;
94-
[self fetchImageWithMetadata:metadata operation:operation options:options context:context progress:progressBlock completed:completedBlock];
94+
[self fetchImageWithMetadata:metadata operation:operation url:url options:options context:context progress:progressBlock completed:completedBlock];
9595
}];
9696
operation.provider = provider;
9797
}
9898

9999
return operation;
100100
}
101101

102-
- (void)fetchImageWithMetadata:(LPLinkMetadata *)metadata operation:(SDImageLinkLoaderOperation *)operation options:(SDWebImageOptions)options context:(SDWebImageContext *)context progress:(SDImageLoaderProgressBlock)progressBlock completed:(SDImageLoaderCompletedBlock)completedBlock {
103-
NSURL *url = metadata.originalURL;
102+
- (void)fetchImageWithMetadata:(LPLinkMetadata *)metadata operation:(SDImageLinkLoaderOperation *)operation url:(NSURL *)url options:(SDWebImageOptions)options context:(SDWebImageContext *)context progress:(SDImageLoaderProgressBlock)progressBlock completed:(SDImageLoaderCompletedBlock)completedBlock {
104103
// Parse context option
105104
BOOL requestData = [context[SDWebImageContextLinkRequestImageData] boolValue];
106105
// Check image provider

0 commit comments

Comments
 (0)