|
9 | 9 | #import "LPLinkView+WebCache.h" |
10 | 10 | #import "SDWebImageLinkDefine.h" |
11 | 11 |
|
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 | +} |
16 | 19 |
|
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 | +} |
18 | 45 |
|
19 | 46 | @implementation LPLinkView (WebCache) |
20 | 47 |
|
@@ -73,9 +100,15 @@ - (void)sd_setImageWithURL:(nullable NSURL *)url |
73 | 100 | metadata.originalURL = url; |
74 | 101 | metadata.URL = imageURL; |
75 | 102 | } |
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); |
79 | 112 | } else { |
80 | 113 | metadata = [[LPLinkMetadata alloc] init]; |
81 | 114 | metadata.originalURL = url; |
|
0 commit comments