From 8742e21fab0666afa8a084d12bce095be252bda3 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Mon, 19 Feb 2018 20:10:14 +0800 Subject: [PATCH] Do not expose the separator because of extensibility, use a function instead --- SDWebImage/SDImageCache.m | 2 +- SDWebImage/SDWebImageManager.m | 2 +- SDWebImage/SDWebImageTransformer.h | 12 +++++++++--- SDWebImage/SDWebImageTransformer.m | 12 ++++++++++-- SDWebImage/UIImage+Transform.m | 8 ++++---- 5 files changed, 25 insertions(+), 11 deletions(-) diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index a16b4600..d790aef8 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -538,7 +538,7 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { // grab the transformed disk image if transformer provided id transformer = [context valueForKey:SDWebImageContextCustomTransformer]; NSString *transformerKey = [transformer transformerKey]; - cacheKey = [[key stringByAppendingString:SDWebImageTransformerKeySeparator] stringByAppendingString:transformerKey]; + cacheKey = SDTransformedKeyForKey(key, transformerKey); } // decode image data only if in-memory cache missed diskImage = [self diskImageForKey:cacheKey data:diskData]; diff --git a/SDWebImage/SDWebImageManager.m b/SDWebImage/SDWebImageManager.m index c30a612d..f92ed841 100644 --- a/SDWebImage/SDWebImageManager.m +++ b/SDWebImage/SDWebImageManager.m @@ -255,7 +255,7 @@ UIImage *transformedImage = [transformer transformedImageWithImage:downloadedImage forKey:key]; if (transformedImage && finished) { NSString *transformerKey = [transformer transformerKey]; - NSString *cacheKey = [[key stringByAppendingString:SDWebImageTransformerKeySeparator] stringByAppendingString:transformerKey]; + NSString *cacheKey = SDTransformedKeyForKey(key, transformerKey); BOOL imageWasTransformed = ![transformedImage isEqual:downloadedImage]; NSData *cacheData; // pass nil if the image was transformed, so we can recalculate the data from the image diff --git a/SDWebImage/SDWebImageTransformer.h b/SDWebImage/SDWebImageTransformer.h index a060e6a3..624bbe4d 100644 --- a/SDWebImage/SDWebImageTransformer.h +++ b/SDWebImage/SDWebImageTransformer.h @@ -9,6 +9,15 @@ #import "SDWebImageCompat.h" #import "UIImage+Transform.h" +/** + Return the transformed cache key which applied with specify transformerKey. + + @param key The original cache key + @param transformerKey The transformer key from the transformer + @return The transformed cache key + */ +FOUNDATION_EXPORT NSString * _Nullable SDTransformedKeyForKey(NSString * _Nullable key, NSString * _Nonnull transformerKey); + /** A transformer protocol to transform the image load from cache or from download. You can provide transformer to cache and manager (Through the `transformer` property or context option `SDWebImageContextCustomTransformer`). @@ -38,9 +47,6 @@ #pragma mark - Pipeline -// Separator for different transformerKey, for example, `image.png` |> flip(YES,NO) |> rotate(pi/4,YES) => 'image-SDWebImageFlippingTransformer(1,0)-SDWebImageRotationTransformer(0.78539816339,1).png' -FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageTransformerKeySeparator; - // Pipeline transformer. Which you can bind multiple transformers together to let the image to be transformed one by one in order and generate the final image. @interface SDWebImagePipelineTransformer : NSObject diff --git a/SDWebImage/SDWebImageTransformer.m b/SDWebImage/SDWebImageTransformer.m index 13da7fed..b7d63015 100644 --- a/SDWebImage/SDWebImageTransformer.m +++ b/SDWebImage/SDWebImageTransformer.m @@ -11,6 +11,16 @@ #import #endif +// Separator for different transformerKey, for example, `image.png` |> flip(YES,NO) |> rotate(pi/4,YES) => 'image-SDWebImageFlippingTransformer(1,0)-SDWebImageRotationTransformer(0.78539816339,1).png' +static NSString * const SDWebImageTransformerKeySeparator = @"-"; + +NSString * _Nullable SDTransformedKeyForKey(NSString * _Nullable key, NSString * _Nonnull transformerKey) { + if (!key || !transformerKey) { + return nil; + } + return [[key stringByAppendingString:SDWebImageTransformerKeySeparator] stringByAppendingString:transformerKey]; +} + @interface UIColor (Additions) @property (nonatomic, copy, readonly, nonnull) NSString *sd_hexString; @@ -50,8 +60,6 @@ @end -NSString * const SDWebImageTransformerKeySeparator = @"-"; - @interface SDWebImagePipelineTransformer () @property (nonatomic, copy, readwrite, nonnull) NSArray> *transformers; diff --git a/SDWebImage/UIImage+Transform.m b/SDWebImage/UIImage+Transform.m index ecacfd09..543ed2b0 100644 --- a/SDWebImage/UIImage+Transform.m +++ b/SDWebImage/UIImage+Transform.m @@ -151,13 +151,13 @@ static CGRect SDCGRectFitWithScaleMode(CGRect rect, CGSize size, SDImageScaleMod #if SD_MAC @interface NSBezierPath (Additions) -+ (instancetype)bezierPathWithRoundedRect:(NSRect)rect byRoundingCorners:(SDRectCorner)corners cornerRadius:(CGFloat)cornerRadius; ++ (instancetype)sd_bezierPathWithRoundedRect:(NSRect)rect byRoundingCorners:(SDRectCorner)corners cornerRadius:(CGFloat)cornerRadius; @end @implementation NSBezierPath (Additions) -+ (instancetype)bezierPathWithRoundedRect:(NSRect)rect byRoundingCorners:(SDRectCorner)corners cornerRadius:(CGFloat)cornerRadius { ++ (instancetype)sd_bezierPathWithRoundedRect:(NSRect)rect byRoundingCorners:(SDRectCorner)corners cornerRadius:(CGFloat)cornerRadius { NSBezierPath *path = [NSBezierPath bezierPath]; CGFloat maxCorner = MIN(NSWidth(rect), NSHeight(rect)) / 2; @@ -246,7 +246,7 @@ static CGRect SDCGRectFitWithScaleMode(CGRect rect, CGSize size, SDImageScaleMod #if SD_UIKIT || SD_WATCH UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectInset(rect, borderWidth, borderWidth) byRoundingCorners:corners cornerRadii:CGSizeMake(cornerRadius, cornerRadius)]; #else - NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:CGRectInset(rect, borderWidth, borderWidth) byRoundingCorners:corners cornerRadius:cornerRadius]; + NSBezierPath *path = [NSBezierPath sd_bezierPathWithRoundedRect:CGRectInset(rect, borderWidth, borderWidth) byRoundingCorners:corners cornerRadius:cornerRadius]; #endif [path closePath]; @@ -263,7 +263,7 @@ static CGRect SDCGRectFitWithScaleMode(CGRect rect, CGSize size, SDImageScaleMod #if SD_UIKIT || SD_WATCH UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:strokeRect byRoundingCorners:corners cornerRadii:CGSizeMake(strokeRadius, strokeRadius)]; #else - NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:strokeRect byRoundingCorners:corners cornerRadius:strokeRadius]; + NSBezierPath *path = [NSBezierPath sd_bezierPathWithRoundedRect:strokeRect byRoundingCorners:corners cornerRadius:strokeRadius]; #endif [path closePath];