Skip to content

Commit 2245b38

Browse files
authored
Merge pull request #4 from SDWebImage/feature_cache_metadata
Feature: Supports to cache the `LPLinkMetadata` as well as image
2 parents 29aef9e + e5a98ad commit 2245b38

13 files changed

Lines changed: 129 additions & 140 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ Carthage
3333
# `pod install` in .travis.yml
3434
#
3535
Pods/
36+
Podfile.lock

Example/Podfile.lock

Lines changed: 0 additions & 29 deletions
This file was deleted.

Example/SDWebImageLinkPlugin/SDViewController.m

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ @implementation LinkTableViewCell
2121

2222
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
2323
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
24-
NSURL *url = nil;
25-
_linkView = [[LPLinkView alloc] initWithURL:url]; // We must pass nil here, or will cause Cell-reusing issues on iOS 13.1.
24+
LPLinkMetadata *metadata = [LPLinkMetadata new]; // We must pass empty metadata here, or will cause Cell-reusing issues on iOS 13.1.
25+
_linkView = [[LPLinkView alloc] initWithMetadata:metadata];
2626
[self.contentView addSubview:_linkView];
2727
}
2828
return self;
@@ -37,7 +37,8 @@ - (void)layoutSubviews {
3737

3838
@interface ImageTableViewCell : UITableViewCell
3939

40-
@property (nonatomic, strong) UILabel *customTextLabel;
40+
@property (nonatomic, strong) UILabel *hostLabel;
41+
@property (nonatomic, strong) UILabel *titleLabel;
4142
@property (nonatomic, strong) UIImageView *customImageView;
4243

4344
@end
@@ -51,17 +52,22 @@ - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSStr
5152
_customImageView.clipsToBounds = YES;
5253
_customImageView.layer.cornerRadius = 10;
5354
[self.contentView addSubview:_customImageView];
54-
_customTextLabel = [[UILabel alloc] init];
55-
[self.contentView addSubview:_customTextLabel];
55+
_hostLabel = [[UILabel alloc] init];
56+
_hostLabel.font = [UIFont systemFontOfSize:12];
57+
[self.contentView addSubview:_hostLabel];
58+
_titleLabel = [[UILabel alloc] init];
59+
_titleLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightBold];
60+
[self.contentView addSubview:_titleLabel];
5661
}
5762
return self;
5863
}
5964

6065
- (void)layoutSubviews {
6166
[super layoutSubviews];
62-
self.customImageView.frame = CGRectInset(self.bounds, 20, 20);
63-
[self.customTextLabel sizeToFit];
64-
self.customTextLabel.frame = CGRectMake(20, self.bounds.size.height - 20, self.bounds.size.width - 40, self.customTextLabel.frame.size.height);
67+
self.customImageView.frame = CGRectInset(self.bounds, 20, 40);
68+
self.hostLabel.frame = CGRectMake(30, self.bounds.size.height - 20, self.bounds.size.width - 2 * 30, 20);
69+
self.titleLabel.frame = CGRectMake(30, self.bounds.size.height - 40, self.bounds.size.width - 2 * 30, 20);
70+
6571
}
6672

6773
@end
@@ -87,7 +93,7 @@ - (void)viewDidLoad {
8793
style:UIBarButtonItemStylePlain
8894
target:self
8995
action:@selector(switchView)];
90-
self.useLinkView = NO;
96+
self.useLinkView = YES;
9197
self.objects = [NSArray arrayWithObjects:
9298
@"https://www.apple.com/",
9399
@"https://www.apple.com/music/",
@@ -142,13 +148,24 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
142148
if (![cell isKindOfClass:LinkTableViewCell.class]) {
143149
cell = [[LinkTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
144150
}
151+
((LinkTableViewCell *)cell).linkView.sd_imageTransition = SDWebImageTransition.fadeTransition;
145152
[((LinkTableViewCell *)cell).linkView sd_setImageWithURL:url];
146153
} else {
147154
if (![cell isKindOfClass:ImageTableViewCell.class]) {
148155
cell = [[ImageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
149156
}
150-
((ImageTableViewCell *)cell).customTextLabel.text = url.host;
151-
[((ImageTableViewCell *)cell).customImageView sd_setImageWithURL:url];
157+
((ImageTableViewCell *)cell).hostLabel.text = url.host;
158+
((ImageTableViewCell *)cell).titleLabel.text = nil;
159+
((ImageTableViewCell *)cell).customImageView.sd_imageTransition = SDWebImageTransition.fadeTransition;
160+
[((ImageTableViewCell *)cell).customImageView sd_setImageWithURL:url completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
161+
if (image) {
162+
if ([image.sd_extendedObject isKindOfClass:LPLinkMetadata.class]) {
163+
LPLinkMetadata *metadata = (LPLinkMetadata *)image.sd_extendedObject;
164+
((ImageTableViewCell *)cell).titleLabel.text = metadata.title;
165+
((ImageTableViewCell *)cell).hostLabel.text = metadata.URL.host;
166+
}
167+
}
168+
}];
152169
}
153170

154171
return cell;

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.

SDWebImageLinkPlugin.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ SDWebImageLinkPlugin is a plugin for SDWebImage framework, which provide the ima
3636
'DERIVE_MACCATALYST_PRODUCT_BUNDLE_IDENTIFIER' => 'NO'
3737
}
3838

39-
s.dependency 'SDWebImage', '~> 5.0'
39+
s.dependency 'SDWebImage', '~> 5.4'
4040
end

SDWebImageLinkPlugin.xcodeproj/project.pbxproj

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,12 @@
1010
3253E0BB2387DCB2007ACAD8 /* SDWebImageLinkPlugin.h in Headers */ = {isa = PBXBuildFile; fileRef = 3253E0B92387DCB2007ACAD8 /* SDWebImageLinkPlugin.h */; settings = {ATTRIBUTES = (Public, ); }; };
1111
3253E0CB2387E6B4007ACAD8 /* SDImageLinkLoader.m in Sources */ = {isa = PBXBuildFile; fileRef = 3253E0C22387E6B4007ACAD8 /* SDImageLinkLoader.m */; };
1212
3253E0CC2387E6B4007ACAD8 /* SDWebImageLinkDefine.m in Sources */ = {isa = PBXBuildFile; fileRef = 3253E0C32387E6B4007ACAD8 /* SDWebImageLinkDefine.m */; };
13-
3253E0CE2387E6B4007ACAD8 /* NSURL+SDWebImageLinkPlugin.h in Headers */ = {isa = PBXBuildFile; fileRef = 3253E0C52387E6B4007ACAD8 /* NSURL+SDWebImageLinkPlugin.h */; settings = {ATTRIBUTES = (Public, ); }; };
1413
3253E0CF2387E6B4007ACAD8 /* SDWebImageLinkError.h in Headers */ = {isa = PBXBuildFile; fileRef = 3253E0C62387E6B4007ACAD8 /* SDWebImageLinkError.h */; settings = {ATTRIBUTES = (Public, ); }; };
1514
3253E0D02387E6B4007ACAD8 /* SDImageLinkLoader.h in Headers */ = {isa = PBXBuildFile; fileRef = 3253E0C72387E6B4007ACAD8 /* SDImageLinkLoader.h */; settings = {ATTRIBUTES = (Public, ); }; };
16-
3253E0D12387E6B4007ACAD8 /* NSURL+SDWebImageLinkPlugin.m in Sources */ = {isa = PBXBuildFile; fileRef = 3253E0C82387E6B4007ACAD8 /* NSURL+SDWebImageLinkPlugin.m */; };
1715
3253E0D22387E6B4007ACAD8 /* SDWebImageLinkDefine.h in Headers */ = {isa = PBXBuildFile; fileRef = 3253E0C92387E6B4007ACAD8 /* SDWebImageLinkDefine.h */; settings = {ATTRIBUTES = (Public, ); }; };
1816
3253E0D32387E6B4007ACAD8 /* SDWebImageLinkError.m in Sources */ = {isa = PBXBuildFile; fileRef = 3253E0CA2387E6B4007ACAD8 /* SDWebImageLinkError.m */; };
1917
3253E0D62387E8C5007ACAD8 /* SDWebImage.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3253E0D52387E8C5007ACAD8 /* SDWebImage.framework */; };
2018
3253E0E72387E900007ACAD8 /* SDWebImage.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3253E0E62387E900007ACAD8 /* SDWebImage.framework */; };
21-
3253E0EA2387E94D007ACAD8 /* NSURL+SDWebImageLinkPlugin.h in Headers */ = {isa = PBXBuildFile; fileRef = 3253E0C52387E6B4007ACAD8 /* NSURL+SDWebImageLinkPlugin.h */; settings = {ATTRIBUTES = (Public, ); }; };
22-
3253E0EB2387E94D007ACAD8 /* NSURL+SDWebImageLinkPlugin.m in Sources */ = {isa = PBXBuildFile; fileRef = 3253E0C82387E6B4007ACAD8 /* NSURL+SDWebImageLinkPlugin.m */; };
2319
3253E0EC2387E94D007ACAD8 /* SDImageLinkLoader.h in Headers */ = {isa = PBXBuildFile; fileRef = 3253E0C72387E6B4007ACAD8 /* SDImageLinkLoader.h */; settings = {ATTRIBUTES = (Public, ); }; };
2420
3253E0ED2387E94D007ACAD8 /* SDImageLinkLoader.m in Sources */ = {isa = PBXBuildFile; fileRef = 3253E0C22387E6B4007ACAD8 /* SDImageLinkLoader.m */; };
2521
3253E0EE2387E94D007ACAD8 /* SDWebImageLinkDefine.h in Headers */ = {isa = PBXBuildFile; fileRef = 3253E0C92387E6B4007ACAD8 /* SDWebImageLinkDefine.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -43,10 +39,8 @@
4339
3253E0BA2387DCB2007ACAD8 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = Info.plist; path = Module/Info.plist; sourceTree = "<group>"; };
4440
3253E0C22387E6B4007ACAD8 /* SDImageLinkLoader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDImageLinkLoader.m; sourceTree = "<group>"; };
4541
3253E0C32387E6B4007ACAD8 /* SDWebImageLinkDefine.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDWebImageLinkDefine.m; sourceTree = "<group>"; };
46-
3253E0C52387E6B4007ACAD8 /* NSURL+SDWebImageLinkPlugin.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSURL+SDWebImageLinkPlugin.h"; sourceTree = "<group>"; };
4742
3253E0C62387E6B4007ACAD8 /* SDWebImageLinkError.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDWebImageLinkError.h; sourceTree = "<group>"; };
4843
3253E0C72387E6B4007ACAD8 /* SDImageLinkLoader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDImageLinkLoader.h; sourceTree = "<group>"; };
49-
3253E0C82387E6B4007ACAD8 /* NSURL+SDWebImageLinkPlugin.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSURL+SDWebImageLinkPlugin.m"; sourceTree = "<group>"; };
5044
3253E0C92387E6B4007ACAD8 /* SDWebImageLinkDefine.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDWebImageLinkDefine.h; sourceTree = "<group>"; };
5145
3253E0CA2387E6B4007ACAD8 /* SDWebImageLinkError.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDWebImageLinkError.m; sourceTree = "<group>"; };
5246
3253E0D52387E8C5007ACAD8 /* SDWebImage.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SDWebImage.framework; path = Carthage/Build/iOS/SDWebImage.framework; sourceTree = "<group>"; };
@@ -113,8 +107,6 @@
113107
3253E12E23882AD5007ACAD8 /* LPLinkView+WebCache.m */,
114108
3253E1252387EDE7007ACAD8 /* NSImage+SDWebImageLinkPlugin.h */,
115109
3253E1242387EDE7007ACAD8 /* NSImage+SDWebImageLinkPlugin.m */,
116-
3253E0C52387E6B4007ACAD8 /* NSURL+SDWebImageLinkPlugin.h */,
117-
3253E0C82387E6B4007ACAD8 /* NSURL+SDWebImageLinkPlugin.m */,
118110
3253E0C72387E6B4007ACAD8 /* SDImageLinkLoader.h */,
119111
3253E0C22387E6B4007ACAD8 /* SDImageLinkLoader.m */,
120112
3253E0C92387E6B4007ACAD8 /* SDWebImageLinkDefine.h */,
@@ -143,7 +135,6 @@
143135
files = (
144136
3253E1282387EDE7007ACAD8 /* NSImage+SDWebImageLinkPlugin.h in Headers */,
145137
3253E0D02387E6B4007ACAD8 /* SDImageLinkLoader.h in Headers */,
146-
3253E0CE2387E6B4007ACAD8 /* NSURL+SDWebImageLinkPlugin.h in Headers */,
147138
3253E0D22387E6B4007ACAD8 /* SDWebImageLinkDefine.h in Headers */,
148139
3253E13223882AD5007ACAD8 /* LPLinkView+WebCache.h in Headers */,
149140
3253E0BB2387DCB2007ACAD8 /* SDWebImageLinkPlugin.h in Headers */,
@@ -158,7 +149,6 @@
158149
3253E1292387EDE7007ACAD8 /* NSImage+SDWebImageLinkPlugin.h in Headers */,
159150
3253E0EE2387E94D007ACAD8 /* SDWebImageLinkDefine.h in Headers */,
160151
3253E0EC2387E94D007ACAD8 /* SDImageLinkLoader.h in Headers */,
161-
3253E0EA2387E94D007ACAD8 /* NSURL+SDWebImageLinkPlugin.h in Headers */,
162152
3253E13323882AD5007ACAD8 /* LPLinkView+WebCache.h in Headers */,
163153
3253E0F02387E94D007ACAD8 /* SDWebImageLinkError.h in Headers */,
164154
3253E0F22387E951007ACAD8 /* SDWebImageLinkPlugin.h in Headers */,
@@ -267,7 +257,6 @@
267257
3253E13023882AD5007ACAD8 /* LPLinkView+WebCache.m in Sources */,
268258
3253E1262387EDE7007ACAD8 /* NSImage+SDWebImageLinkPlugin.m in Sources */,
269259
3253E0CB2387E6B4007ACAD8 /* SDImageLinkLoader.m in Sources */,
270-
3253E0D12387E6B4007ACAD8 /* NSURL+SDWebImageLinkPlugin.m in Sources */,
271260
);
272261
runOnlyForDeploymentPostprocessing = 0;
273262
};
@@ -276,7 +265,6 @@
276265
buildActionMask = 2147483647;
277266
files = (
278267
3253E0F12387E94D007ACAD8 /* SDWebImageLinkError.m in Sources */,
279-
3253E0EB2387E94D007ACAD8 /* NSURL+SDWebImageLinkPlugin.m in Sources */,
280268
3253E13123882AD5007ACAD8 /* LPLinkView+WebCache.m in Sources */,
281269
3253E1272387EDE7007ACAD8 /* NSImage+SDWebImageLinkPlugin.m in Sources */,
282270
3253E0EF2387E94D007ACAD8 /* SDWebImageLinkDefine.m in Sources */,

0 commit comments

Comments
 (0)