Skip to content

Commit

Permalink
支持对更多SDWebImage API的拦截,来最大程度的丰富vr
Browse files Browse the repository at this point in the history
  • Loading branch information
Hulk committed Jun 1, 2023
1 parent 92ba3ca commit c4e57af
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ + (NSString*)getViewContentOfControl:(UIControl*)control {

#pragma mark - private method
+ (NSString*)getViewContentOfButton:(UIButton*)button {
SEL sd_selector = NSSelectorFromString(@"sd_imageURL");
if (button.titleLabel.text.length) {
return [NSString stringWithFormat:@"%@%@", kViewRepresentativeContentTypeText, button.titleLabel.text];
}
Expand All @@ -88,6 +89,15 @@ + (NSString*)getViewContentOfButton:(UIButton*)button {
else if (button.imageView.image && button.imageView.image.prismAutoDotImageName.length) {
return [NSString stringWithFormat:@"%@%@", kViewRepresentativeContentTypeLocalImage, button.imageView.image.prismAutoDotImageName];
}
else if (button.imageView.image && button.imageView.image.prismAutoDotImageUrl.length) {
return [NSString stringWithFormat:@"%@%@", kViewRepresentativeContentTypeNetworkImage, button.imageView.image.prismAutoDotImageUrl];
}
else if (button.imageView && [button.imageView respondsToSelector:sd_selector]) {
NSURL *imageUrl = [button.imageView performSelector:sd_selector];
if ([imageUrl isKindOfClass:[NSURL class]] && imageUrl.absoluteString.length) {
return [NSString stringWithFormat:@"%@%@", kViewRepresentativeContentTypeNetworkImage, imageUrl.absoluteString];
}
}
return nil;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ + (NSString*)getRepresentativeContentOfView:(UIView*)view needRecursive:(BOOL)ne
if (imageView.image.prismAutoDotImageName.length) {
return [NSString stringWithFormat:@"%@%@", kViewRepresentativeContentTypeLocalImage, imageView.image.prismAutoDotImageName];
}
else if (imageView.image.prismAutoDotImageUrl.length) {
return [NSString stringWithFormat:@"%@%@", kViewRepresentativeContentTypeNetworkImage, imageView.image.prismAutoDotImageUrl];
}
else if ([imageView respondsToSelector:sd_selector]) {
NSURL *imageUrl = [imageView performSelector:sd_selector];
if ([imageUrl isKindOfClass:[NSURL class]] && imageUrl.absoluteString.length) {
Expand Down
20 changes: 20 additions & 0 deletions iOS/DiDiPrism/Src/Core/Intercept/SDImageCache+PrismIntercept.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// SDImageCache+PrismIntercept.h
// DiDiPrism
//
// Created by hulk on 2023/6/1.
//

#if __has_include(<SDWebImage/SDImageCache.h>)

#import <SDWebImage/SDImageCache.h>

NS_ASSUME_NONNULL_BEGIN

@interface SDImageCache (PrismIntercept)
+ (void)prism_swizzleMethodIMP;
@end

NS_ASSUME_NONNULL_END

#endif
57 changes: 57 additions & 0 deletions iOS/DiDiPrism/Src/Core/Intercept/SDImageCache+PrismIntercept.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// SDImageCache+PrismIntercept.m
// DiDiPrism
//
// Created by hulk on 2023/6/1.
//

#if __has_include(<SDWebImage/SDImageCache.h>)

#import "SDImageCache+PrismIntercept.h"
#import "PrismRuntimeUtil.h"
#import "UIImage+PrismIntercept.h"

@implementation SDImageCache (PrismIntercept)
#pragma mark - public method
+ (void)prism_swizzleMethodIMP {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[PrismRuntimeUtil hookClass:[self class] originalSelector:@selector(storeImage:imageData:forKey:toMemory:toDisk:completion:) swizzledSelector:@selector(prism_autoDot_storeImage:imageData:forKey:toMemory:toDisk:completion:)];
[PrismRuntimeUtil hookClass:[self class] originalSelector:@selector(imageFromMemoryCacheForKey:) swizzledSelector:@selector(prism_autoDot_imageFromMemoryCacheForKey:)];
[PrismRuntimeUtil hookClass:[self class] originalSelector:@selector(diskImageForKey:data:options:context:) swizzledSelector:@selector(prism_autoDot_diskImageForKey:data:options:context:)];

});
}

#pragma mark - private method
- (void)prism_autoDot_storeImage:(nullable UIImage *)image
imageData:(nullable NSData *)imageData
forKey:(nullable NSString *)key
toMemory:(BOOL)toMemory
toDisk:(BOOL)toDisk
completion:(nullable SDWebImageNoParamsBlock)completionBlock {
if (image && [image isKindOfClass:[UIImage class]] && key) {
image.prismAutoDotImageUrl = key;
}

[self prism_autoDot_storeImage:image imageData:imageData forKey:key toMemory:toMemory toDisk:toDisk completion:completionBlock];
}

- (nullable UIImage *)prism_autoDot_imageFromMemoryCacheForKey:(nullable NSString *)key {
UIImage *image = [self prism_autoDot_imageFromMemoryCacheForKey:key];
if (image && [image isKindOfClass:[UIImage class]] && key) {
image.prismAutoDotImageUrl = key;
}
return image;
}

- (nullable UIImage *)prism_autoDot_diskImageForKey:(nullable NSString *)key data:(nullable NSData *)data options:(SDImageCacheOptions)options context:(SDWebImageContext *)context {
UIImage *image = [self prism_autoDot_diskImageForKey:key data:data options:options context:context];
if (image && [image isKindOfClass:[UIImage class]] && key) {
image.prismAutoDotImageUrl = key;
}
return image;
}
@end

#endif
1 change: 1 addition & 0 deletions iOS/DiDiPrism/Src/Core/Intercept/UIImage+PrismIntercept.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ NS_ASSUME_NONNULL_BEGIN
@interface UIImage (PrismIntercept)

@property (nonatomic, copy) NSString *prismAutoDotImageName;
@property (nonatomic, copy) NSString *prismAutoDotImageUrl;

+ (void)prism_swizzleMethodIMP;
@end
Expand Down
8 changes: 8 additions & 0 deletions iOS/DiDiPrism/Src/Core/Intercept/UIImage+PrismIntercept.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,12 @@ - (void)setPrismAutoDotImageName:(NSString *)prismAutoDotImageName {
objc_setAssociatedObject(self, @selector(prismAutoDotImageName), prismAutoDotImageName, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (NSString *)prismAutoDotImageUrl {
NSString *name = objc_getAssociatedObject(self, _cmd);
return name;
}
- (void)setPrismAutoDotImageUrl:(NSString *)prismAutoDotImageUrl {
objc_setAssociatedObject(self, @selector(prismAutoDotImageUrl), prismAutoDotImageUrl, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

@end
4 changes: 4 additions & 0 deletions iOS/DiDiPrism/Src/Core/PrismEngine.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// Category
#import "UIControl+PrismIntercept.h"
#import "UIImage+PrismIntercept.h"
#import "SDImageCache+PrismIntercept.h"
#import "UILongPressGestureRecognizer+PrismIntercept.h"
#import "UITapGestureRecognizer+PrismIntercept.h"
#import "UIView+PrismIntercept.h"
Expand All @@ -24,6 +25,9 @@ @implementation PrismEngine
#pragma mark public method
+ (void)startEngineWithEventCategorys:(PrismEventCategory)eventCategorys {
[UIImage prism_swizzleMethodIMP];
#if __has_include(<SDWebImage/SDImageCache.h>)
[SDImageCache prism_swizzleMethodIMP];
#endif

if (eventCategorys & PrismEventCategoryTouch) {
[UIControl prism_swizzleMethodIMP];
Expand Down

0 comments on commit c4e57af

Please sign in to comment.