From 9fb9ac7ad4b74a2ca286b956d0b5b2d64cea0cbb Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Sat, 9 Mar 2019 17:07:47 +0800 Subject: [PATCH] Fix the current transformer cache key generating rules, try to keep the image file extension as much as we can --- SDWebImage/SDImageTransformer.m | 18 +++++++++++++- Tests/Tests/SDImageTransformerTests.m | 36 +++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/SDWebImage/SDImageTransformer.m b/SDWebImage/SDImageTransformer.m index 0b0b58d7..bca1b68f 100644 --- a/SDWebImage/SDImageTransformer.m +++ b/SDWebImage/SDImageTransformer.m @@ -18,7 +18,23 @@ NSString * _Nullable SDTransformedKeyForKey(NSString * _Nullable key, NSString * if (!key || !transformerKey) { return nil; } - return [[key stringByAppendingString:SDImageTransformerKeySeparator] stringByAppendingString:transformerKey]; + // Find the file extension + NSURL *keyURL = [NSURL URLWithString:key]; + NSString *ext = keyURL ? keyURL.pathExtension : key.pathExtension; + if (ext.length > 0) { + // For non-file URL + if (keyURL && !keyURL.isFileURL) { + // keep anything except path (like URL query) + NSURLComponents *component = [NSURLComponents componentsWithURL:keyURL resolvingAgainstBaseURL:NO]; + component.path = [[[component.path.stringByDeletingPathExtension stringByAppendingString:SDImageTransformerKeySeparator] stringByAppendingString:transformerKey] stringByAppendingPathExtension:ext]; + return component.URL.absoluteString; + } else { + // file URL + return [[[key.stringByDeletingPathExtension stringByAppendingString:SDImageTransformerKeySeparator] stringByAppendingString:transformerKey] stringByAppendingPathExtension:ext]; + } + } else { + return [[key stringByAppendingString:SDImageTransformerKeySeparator] stringByAppendingString:transformerKey]; + } } @interface UIColor (HexString) diff --git a/Tests/Tests/SDImageTransformerTests.m b/Tests/Tests/SDImageTransformerTests.m index bcbfd7b8..57a19589 100644 --- a/Tests/Tests/SDImageTransformerTests.m +++ b/Tests/Tests/SDImageTransformerTests.m @@ -148,6 +148,42 @@ expect(CGSizeEqualToSize(transformedImage.size, size)).beTruthy(); } +- (void)test10TransformerKeyForCacheKey { + NSString *transformerKey = @"SDImageFlippingTransformer(1,0)"; + + // File path representation test cases + NSString *key = @"image.png"; + expect(SDTransformedKeyForKey(key, transformerKey)).equal(@"image-SDImageFlippingTransformer(1,0).png"); + + key = @"image"; + expect(SDTransformedKeyForKey(key, transformerKey)).equal(@"image-SDImageFlippingTransformer(1,0)"); + + key = @".image"; + expect(SDTransformedKeyForKey(key, transformerKey)).equal(@".image-SDImageFlippingTransformer(1,0)"); + + key = @"image."; + expect(SDTransformedKeyForKey(key, transformerKey)).equal(@"image.-SDImageFlippingTransformer(1,0)"); + + key = @"Test/image.png"; + expect(SDTransformedKeyForKey(key, transformerKey)).equal(@"Test/image-SDImageFlippingTransformer(1,0).png"); + + // URL representation test cases + key = @"http://foo/image.png"; + expect(SDTransformedKeyForKey(key, transformerKey)).equal(@"http://foo/image-SDImageFlippingTransformer(1,0).png"); + + key = @"http://foo/image"; + expect(SDTransformedKeyForKey(key, transformerKey)).equal(@"http://foo/image-SDImageFlippingTransformer(1,0)"); + + key = @"http://foo/.image"; + expect(SDTransformedKeyForKey(key, transformerKey)).equal(@"http://foo/.image-SDImageFlippingTransformer(1,0)"); + + key = @"http://foo/image.png?foo=bar#mark"; + expect(SDTransformedKeyForKey(key, transformerKey)).equal(@"http://foo/image-SDImageFlippingTransformer(1,0).png?foo=bar#mark"); + + key = @"ftp://root:password@foo.com/image.png"; + expect(SDTransformedKeyForKey(key, transformerKey)).equal(@"ftp://root:password@foo.com/image-SDImageFlippingTransformer(1,0).png"); +} + #pragma mark - Helper - (UIImage *)testImage {