Skip to content

Commit 0c57a68

Browse files
committed
Add the LPLinkView category, the sd_linkMetadata associated object, make it useful for metadata setup
1 parent 22f549c commit 0c57a68

13 files changed

Lines changed: 378 additions & 45 deletions

File tree

Example/SDWebImageLinkPlugin/SDViewController.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ - (void)viewDidLoad
3434
[self.view addSubview:self.linkView];
3535
[self.view addSubview:self.imageView];
3636

37+
[self.linkView sd_setImageWithURL:url1 placeholderImage:nil options:SDWebImageFromLoaderOnly completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
38+
NSLog(@"%@", @"LPLinkView metadata load success");
39+
}];
3740
[self.imageView sd_setImageWithURL:url2 completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
38-
NSLog(@"%@", @"LPMetadata image load success");
41+
NSLog(@"%@", @"UIImageView image load success");
3942
}];
4043
}
4144

Example/SDWebImageLinkPlugin_Example macOS/ViewController.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ - (void)viewDidLoad {
3333
[self.view addSubview:self.linkView];
3434
[self.view addSubview:self.imageView];
3535

36+
[self.linkView sd_setImageWithURL:url1 placeholderImage:nil options:0 completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
37+
NSLog(@"%@", @"LPLinkView metadata load success");
38+
}];
3639
[self.imageView sd_setImageWithURL:url2 completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
37-
NSLog(@"%@", @"LPMetadata image load success");
40+
NSLog(@"%@", @"UIImageView image load success");
3841
}];
3942
}
4043

SDWebImageLinkPlugin.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
3253E1272387EDE7007ACAD8 /* NSImage+SDWebImageLinkPlugin.m in Sources */ = {isa = PBXBuildFile; fileRef = 3253E1242387EDE7007ACAD8 /* NSImage+SDWebImageLinkPlugin.m */; };
3232
3253E1282387EDE7007ACAD8 /* NSImage+SDWebImageLinkPlugin.h in Headers */ = {isa = PBXBuildFile; fileRef = 3253E1252387EDE7007ACAD8 /* NSImage+SDWebImageLinkPlugin.h */; settings = {ATTRIBUTES = (Public, ); }; };
3333
3253E1292387EDE7007ACAD8 /* NSImage+SDWebImageLinkPlugin.h in Headers */ = {isa = PBXBuildFile; fileRef = 3253E1252387EDE7007ACAD8 /* NSImage+SDWebImageLinkPlugin.h */; settings = {ATTRIBUTES = (Public, ); }; };
34+
3253E13023882AD5007ACAD8 /* LPLinkView+WebCache.m in Sources */ = {isa = PBXBuildFile; fileRef = 3253E12E23882AD5007ACAD8 /* LPLinkView+WebCache.m */; };
35+
3253E13123882AD5007ACAD8 /* LPLinkView+WebCache.m in Sources */ = {isa = PBXBuildFile; fileRef = 3253E12E23882AD5007ACAD8 /* LPLinkView+WebCache.m */; };
36+
3253E13223882AD5007ACAD8 /* LPLinkView+WebCache.h in Headers */ = {isa = PBXBuildFile; fileRef = 3253E12F23882AD5007ACAD8 /* LPLinkView+WebCache.h */; };
37+
3253E13323882AD5007ACAD8 /* LPLinkView+WebCache.h in Headers */ = {isa = PBXBuildFile; fileRef = 3253E12F23882AD5007ACAD8 /* LPLinkView+WebCache.h */; };
3438
/* End PBXBuildFile section */
3539

3640
/* Begin PBXFileReference section */
@@ -50,6 +54,8 @@
5054
3253E0E62387E900007ACAD8 /* SDWebImage.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SDWebImage.framework; path = Carthage/Build/Mac/SDWebImage.framework; sourceTree = "<group>"; };
5155
3253E1242387EDE7007ACAD8 /* NSImage+SDWebImageLinkPlugin.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSImage+SDWebImageLinkPlugin.m"; sourceTree = "<group>"; };
5256
3253E1252387EDE7007ACAD8 /* NSImage+SDWebImageLinkPlugin.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSImage+SDWebImageLinkPlugin.h"; sourceTree = "<group>"; };
57+
3253E12E23882AD5007ACAD8 /* LPLinkView+WebCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "LPLinkView+WebCache.m"; sourceTree = "<group>"; };
58+
3253E12F23882AD5007ACAD8 /* LPLinkView+WebCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "LPLinkView+WebCache.h"; sourceTree = "<group>"; };
5359
/* End PBXFileReference section */
5460

5561
/* Begin PBXFrameworksBuildPhase section */
@@ -103,6 +109,8 @@
103109
3253E0C12387E6B4007ACAD8 /* Classes */ = {
104110
isa = PBXGroup;
105111
children = (
112+
3253E12F23882AD5007ACAD8 /* LPLinkView+WebCache.h */,
113+
3253E12E23882AD5007ACAD8 /* LPLinkView+WebCache.m */,
106114
3253E1252387EDE7007ACAD8 /* NSImage+SDWebImageLinkPlugin.h */,
107115
3253E1242387EDE7007ACAD8 /* NSImage+SDWebImageLinkPlugin.m */,
108116
3253E0C52387E6B4007ACAD8 /* NSURL+SDWebImageLinkPlugin.h */,
@@ -137,6 +145,7 @@
137145
3253E0D02387E6B4007ACAD8 /* SDImageLinkLoader.h in Headers */,
138146
3253E0CE2387E6B4007ACAD8 /* NSURL+SDWebImageLinkPlugin.h in Headers */,
139147
3253E0D22387E6B4007ACAD8 /* SDWebImageLinkDefine.h in Headers */,
148+
3253E13223882AD5007ACAD8 /* LPLinkView+WebCache.h in Headers */,
140149
3253E0BB2387DCB2007ACAD8 /* SDWebImageLinkPlugin.h in Headers */,
141150
3253E0CF2387E6B4007ACAD8 /* SDWebImageLinkError.h in Headers */,
142151
);
@@ -150,6 +159,7 @@
150159
3253E0EE2387E94D007ACAD8 /* SDWebImageLinkDefine.h in Headers */,
151160
3253E0EC2387E94D007ACAD8 /* SDImageLinkLoader.h in Headers */,
152161
3253E0EA2387E94D007ACAD8 /* NSURL+SDWebImageLinkPlugin.h in Headers */,
162+
3253E13323882AD5007ACAD8 /* LPLinkView+WebCache.h in Headers */,
153163
3253E0F02387E94D007ACAD8 /* SDWebImageLinkError.h in Headers */,
154164
3253E0F22387E951007ACAD8 /* SDWebImageLinkPlugin.h in Headers */,
155165
);
@@ -254,6 +264,7 @@
254264
files = (
255265
3253E0D32387E6B4007ACAD8 /* SDWebImageLinkError.m in Sources */,
256266
3253E0CC2387E6B4007ACAD8 /* SDWebImageLinkDefine.m in Sources */,
267+
3253E13023882AD5007ACAD8 /* LPLinkView+WebCache.m in Sources */,
257268
3253E1262387EDE7007ACAD8 /* NSImage+SDWebImageLinkPlugin.m in Sources */,
258269
3253E0CB2387E6B4007ACAD8 /* SDImageLinkLoader.m in Sources */,
259270
3253E0D12387E6B4007ACAD8 /* NSURL+SDWebImageLinkPlugin.m in Sources */,
@@ -266,6 +277,7 @@
266277
files = (
267278
3253E0F12387E94D007ACAD8 /* SDWebImageLinkError.m in Sources */,
268279
3253E0EB2387E94D007ACAD8 /* NSURL+SDWebImageLinkPlugin.m in Sources */,
280+
3253E13123882AD5007ACAD8 /* LPLinkView+WebCache.m in Sources */,
269281
3253E1272387EDE7007ACAD8 /* NSImage+SDWebImageLinkPlugin.m in Sources */,
270282
3253E0EF2387E94D007ACAD8 /* SDWebImageLinkDefine.m in Sources */,
271283
3253E0ED2387E94D007ACAD8 /* SDImageLinkLoader.m in Sources */,
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
* This file is part of the SDWebImage package.
3+
* (c) Olivier Poitrey <rs@dailymotion.com>
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*/
8+
9+
@import SDWebImage;
10+
@import LinkPresentation;
11+
12+
/**
13+
* Integrates SDWebImage async downloading and caching of remote images with LPLinkView.
14+
*/
15+
@interface LPLinkView (WebCache)
16+
17+
/**
18+
* Set the imageView `image` with an `url`.
19+
*
20+
* The download is asynchronous and cached.
21+
*
22+
* @param url The url for the image.
23+
*/
24+
- (void)sd_setImageWithURL:(nullable NSURL *)url NS_REFINED_FOR_SWIFT;
25+
26+
/**
27+
* Set the imageView `image` with an `url` and a placeholder.
28+
*
29+
* The download is asynchronous and cached.
30+
*
31+
* @param url The url for the image.
32+
* @param placeholder The image to be set initially, until the image request finishes.
33+
* @see sd_setImageWithURL:placeholderImage:options:
34+
*/
35+
- (void)sd_setImageWithURL:(nullable NSURL *)url
36+
placeholderImage:(nullable UIImage *)placeholder NS_REFINED_FOR_SWIFT;
37+
38+
/**
39+
* Set the imageView `image` with an `url`, placeholder and custom options.
40+
*
41+
* The download is asynchronous and cached.
42+
*
43+
* @param url The url for the image.
44+
* @param placeholder The image to be set initially, until the image request finishes.
45+
* @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
46+
*/
47+
- (void)sd_setImageWithURL:(nullable NSURL *)url
48+
placeholderImage:(nullable UIImage *)placeholder
49+
options:(SDWebImageOptions)options NS_REFINED_FOR_SWIFT;
50+
51+
/**
52+
* Set the imageView `image` with an `url`, placeholder, custom options and context.
53+
*
54+
* The download is asynchronous and cached.
55+
*
56+
* @param url The url for the image.
57+
* @param placeholder The image to be set initially, until the image request finishes.
58+
* @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
59+
* @param context A context contains different options to perform specify changes or processes, see `SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold.
60+
*/
61+
- (void)sd_setImageWithURL:(nullable NSURL *)url
62+
placeholderImage:(nullable UIImage *)placeholder
63+
options:(SDWebImageOptions)options
64+
context:(nullable SDWebImageContext *)context;
65+
66+
/**
67+
* Set the imageView `image` with an `url`.
68+
*
69+
* The download is asynchronous and cached.
70+
*
71+
* @param url The url for the image.
72+
* @param completedBlock A block called when operation has been completed. This block has no return value
73+
* and takes the requested UIImage as first parameter. In case of error the image parameter
74+
* is nil and the second parameter may contain an NSError. The third parameter is a Boolean
75+
* indicating if the image was retrieved from the local cache or from the network.
76+
* The fourth parameter is the original image url.
77+
*/
78+
- (void)sd_setImageWithURL:(nullable NSURL *)url
79+
completed:(nullable SDExternalCompletionBlock)completedBlock;
80+
81+
/**
82+
* Set the imageView `image` with an `url`, placeholder.
83+
*
84+
* The download is asynchronous and cached.
85+
*
86+
* @param url The url for the image.
87+
* @param placeholder The image to be set initially, until the image request finishes.
88+
* @param completedBlock A block called when operation has been completed. This block has no return value
89+
* and takes the requested UIImage as first parameter. In case of error the image parameter
90+
* is nil and the second parameter may contain an NSError. The third parameter is a Boolean
91+
* indicating if the image was retrieved from the local cache or from the network.
92+
* The fourth parameter is the original image url.
93+
*/
94+
- (void)sd_setImageWithURL:(nullable NSURL *)url
95+
placeholderImage:(nullable UIImage *)placeholder
96+
completed:(nullable SDExternalCompletionBlock)completedBlock NS_REFINED_FOR_SWIFT;
97+
98+
/**
99+
* Set the imageView `image` with an `url`, placeholder and custom options.
100+
*
101+
* The download is asynchronous and cached.
102+
*
103+
* @param url The url for the image.
104+
* @param placeholder The image to be set initially, until the image request finishes.
105+
* @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
106+
* @param completedBlock A block called when operation has been completed. This block has no return value
107+
* and takes the requested UIImage as first parameter. In case of error the image parameter
108+
* is nil and the second parameter may contain an NSError. The third parameter is a Boolean
109+
* indicating if the image was retrieved from the local cache or from the network.
110+
* The fourth parameter is the original image url.
111+
*/
112+
- (void)sd_setImageWithURL:(nullable NSURL *)url
113+
placeholderImage:(nullable UIImage *)placeholder
114+
options:(SDWebImageOptions)options
115+
completed:(nullable SDExternalCompletionBlock)completedBlock;
116+
117+
/**
118+
* Set the imageView `image` with an `url`, placeholder and custom options.
119+
*
120+
* The download is asynchronous and cached.
121+
*
122+
* @param url The url for the image.
123+
* @param placeholder The image to be set initially, until the image request finishes.
124+
* @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
125+
* @param progressBlock A block called while image is downloading
126+
* @note the progress block is executed on a background queue
127+
* @param completedBlock A block called when operation has been completed. This block has no return value
128+
* and takes the requested UIImage as first parameter. In case of error the image parameter
129+
* is nil and the second parameter may contain an NSError. The third parameter is a Boolean
130+
* indicating if the image was retrieved from the local cache or from the network.
131+
* The fourth parameter is the original image url.
132+
*/
133+
- (void)sd_setImageWithURL:(nullable NSURL *)url
134+
placeholderImage:(nullable UIImage *)placeholder
135+
options:(SDWebImageOptions)options
136+
progress:(nullable SDImageLoaderProgressBlock)progressBlock
137+
completed:(nullable SDExternalCompletionBlock)completedBlock;
138+
139+
/**
140+
* Set the imageView `image` with an `url`, placeholder, custom options and context.
141+
*
142+
* The download is asynchronous and cached.
143+
*
144+
* @param url The url for the image.
145+
* @param placeholder The image to be set initially, until the image request finishes.
146+
* @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
147+
* @param context A context contains different options to perform specify changes or processes, see `SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold.
148+
* @param progressBlock A block called while image is downloading
149+
* @note the progress block is executed on a background queue
150+
* @param completedBlock A block called when operation has been completed. This block has no return value
151+
* and takes the requested UIImage as first parameter. In case of error the image parameter
152+
* is nil and the second parameter may contain an NSError. The third parameter is a Boolean
153+
* indicating if the image was retrieved from the local cache or from the network.
154+
* The fourth parameter is the original image url.
155+
*/
156+
- (void)sd_setImageWithURL:(nullable NSURL *)url
157+
placeholderImage:(nullable UIImage *)placeholder
158+
options:(SDWebImageOptions)options
159+
context:(nullable SDWebImageContext *)context
160+
progress:(nullable SDImageLoaderProgressBlock)progressBlock
161+
completed:(nullable SDExternalCompletionBlock)completedBlock;
162+
163+
@end
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* This file is part of the SDWebImage package.
3+
* (c) Olivier Poitrey <rs@dailymotion.com>
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*/
8+
9+
#import "LPLinkView+WebCache.h"
10+
#import "NSURL+SDWebImageLinkPlugin.h"
11+
#import "NSImage+SDWebImageLinkPlugin.h"
12+
13+
@implementation LPLinkView (WebCache)
14+
15+
- (void)sd_setImageWithURL:(nullable NSURL *)url {
16+
[self sd_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:nil];
17+
}
18+
19+
- (void)sd_setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder {
20+
[self sd_setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:nil];
21+
}
22+
23+
- (void)sd_setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options {
24+
[self sd_setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:nil];
25+
}
26+
27+
- (void)sd_setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options context:(nullable SDWebImageContext *)context {
28+
[self sd_setImageWithURL:url placeholderImage:placeholder options:options context:context progress:nil completed:nil];
29+
}
30+
31+
- (void)sd_setImageWithURL:(nullable NSURL *)url completed:(nullable SDExternalCompletionBlock)completedBlock {
32+
[self sd_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:completedBlock];
33+
}
34+
35+
- (void)sd_setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder completed:(nullable SDExternalCompletionBlock)completedBlock {
36+
[self sd_setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:completedBlock];
37+
}
38+
39+
- (void)sd_setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options completed:(nullable SDExternalCompletionBlock)completedBlock {
40+
[self sd_setImageWithURL:url placeholderImage:placeholder options:options progress:nil completed:completedBlock];
41+
}
42+
43+
- (void)sd_setImageWithURL:(nullable NSURL *)url placeholderImage:(nullable UIImage *)placeholder options:(SDWebImageOptions)options progress:(nullable SDImageLoaderProgressBlock)progressBlock completed:(nullable SDExternalCompletionBlock)completedBlock {
44+
[self sd_setImageWithURL:url placeholderImage:placeholder options:options context:nil progress:progressBlock completed:completedBlock];
45+
}
46+
47+
- (void)sd_setImageWithURL:(nullable NSURL *)url
48+
placeholderImage:(nullable UIImage *)placeholder
49+
options:(SDWebImageOptions)options
50+
context:(nullable SDWebImageContext *)context
51+
progress:(nullable SDImageLoaderProgressBlock)progressBlock
52+
completed:(nullable SDExternalCompletionBlock)completedBlock {
53+
__weak typeof(self) wself = self;
54+
[self sd_internalSetImageWithURL:url placeholderImage:placeholder options:options context:context setImageBlock:^(UIImage * _Nullable image, NSData * _Nullable imageData, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
55+
__strong typeof(self) sself = wself;
56+
LPLinkMetadata *metadata = imageURL.sd_linkMetadata;
57+
if (metadata) {
58+
sself.metadata = metadata;
59+
} else if (image) {
60+
// Re-generate the metadata from local information
61+
metadata = [[LPLinkMetadata alloc] init];
62+
metadata.originalURL = url;
63+
metadata.URL = imageURL;
64+
metadata.imageProvider = [[NSItemProvider alloc] initWithObject:image];
65+
imageURL.sd_linkMetadata = metadata;
66+
sself.metadata = metadata;
67+
}
68+
} progress:progressBlock completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
69+
if (completedBlock) {
70+
completedBlock(image, error, cacheType, imageURL);
71+
}
72+
}];
73+
}
74+
75+
@end

SDWebImageLinkPlugin/Classes/NSImage+SDWebImageLinkPlugin.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
#if SD_MAC
1111

12-
@interface NSImage (SDWebImageLinkPlugin) <NSItemProviderReading>
12+
/// Make NSImage supports the `NSItemProviderReading` as well as UIKit's UIImage
13+
@interface NSImage (SDWebImageLinkPlugin) <NSItemProviderReading, NSItemProviderWriting>
1314

1415
@end
1516

0 commit comments

Comments
 (0)