Skip to content

Commit e5a98ad

Browse files
committed
Update to use SDWebImage 5.4.0 dependency, hide the SPI usage
1 parent 0417eab commit e5a98ad

3 files changed

Lines changed: 42 additions & 11 deletions

File tree

Example/Podfile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use_frameworks!
33
target 'SDWebImageLinkPlugin_Example' do
44
platform :ios, '13.0'
55
pod 'SDWebImageLinkPlugin', :path => '../'
6-
pod 'SDWebImage', :git => 'https://github.com/dreampiggy/SDWebImage.git', :branch => 'feature_extended_metadata_disk_cache'
76

87
target 'SDWebImageLinkPlugin_Tests' do
98
inherit! :search_paths
@@ -14,5 +13,4 @@ end
1413
target 'SDWebImageLinkPlugin_Example macOS' do
1514
platform :osx, '10.15'
1615
pod 'SDWebImageLinkPlugin', :path => '../'
17-
pod 'SDWebImage', :git => 'https://github.com/dreampiggy/SDWebImage.git', :branch => 'feature_extended_metadata_disk_cache'
1816
end

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/Classes/LPLinkView+WebCache.m

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,39 @@
99
#import "LPLinkView+WebCache.h"
1010
#import "SDWebImageLinkDefine.h"
1111

12-
#define LPImageClass @"LPImage"
13-
@protocol LPImageProtocol <NSObject>
14-
15-
- (instancetype)initWithPlatformImage:(UIImage *)image;
12+
static inline NSString *SDBase64DecodedString(NSString *base64String) {
13+
NSData *data = [[NSData alloc] initWithBase64EncodedString:base64String options:NSDataBase64DecodingIgnoreUnknownCharacters];
14+
if (!data) {
15+
return nil;
16+
}
17+
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
18+
}
1619

17-
@end
20+
static inline void SDLinkMetadataSetImage(LPLinkMetadata *metadata, UIImage *image) {
21+
static Class LPImageClass;
22+
static SEL initWithPlatformImageSEL;
23+
if (!LPImageClass) {
24+
LPImageClass = NSClassFromString(SDBase64DecodedString(@"TFBJbWFnZQ=="));
25+
if (!LPImageClass) {
26+
return;
27+
}
28+
}
29+
if (!initWithPlatformImageSEL) {
30+
initWithPlatformImageSEL = NSSelectorFromString(SDBase64DecodedString(@"aW5pdFdpdGhQbGF0Zm9ybUltYWdlOg=="));
31+
if (!initWithPlatformImageSEL) {
32+
return;
33+
}
34+
}
35+
#pragma clang diagnostic push
36+
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
37+
id linkImage = [[LPImageClass alloc] performSelector:initWithPlatformImageSEL withObject:image];
38+
#pragma clang diagnostic pop
39+
@try {
40+
[metadata setValue:linkImage forKey:@"image"];
41+
} @catch (NSException *exception) {
42+
NSLog(@"SDLinkMetadataSetImage error with exception: %@", exception);
43+
}
44+
}
1845

1946
@implementation LPLinkView (WebCache)
2047

@@ -73,9 +100,15 @@ - (void)sd_setImageWithURL:(nullable NSURL *)url
73100
metadata.originalURL = url;
74101
metadata.URL = imageURL;
75102
}
76-
// LPLinkMetadata.imageProvider on iOS 13.1 contains bug which cause async query, and not compatible for cell-reusing. Radar FB7462933
77-
id<LPImageProtocol> linkImage = [[NSClassFromString(LPImageClass) alloc] initWithPlatformImage:image];
78-
[metadata setValue:linkImage forKey:@"image"];
103+
// Create a new UIImage to avoid retain cycle
104+
#if SD_MAC
105+
UIImage *platformImage = [[UIImage alloc] initWithCGImage:image.CGImage scale:image.scale orientation:kCGImagePropertyOrientationUp];
106+
#else
107+
UIImage *platformImage = [[UIImage alloc] initWithCGImage:image.CGImage scale:image.scale orientation:image.imageOrientation];
108+
#endif
109+
// LPLinkMetadata.imageProvider on iOS 13.1 contains bug which cause async query, and not compatible for cell-reusing.
110+
// Here we have to use image instead of imageProvider, Radar FB7462933
111+
SDLinkMetadataSetImage(metadata, platformImage);
79112
} else {
80113
metadata = [[LPLinkMetadata alloc] init];
81114
metadata.originalURL = url;

0 commit comments

Comments
 (0)