diff --git a/SDWebImage/SDImageTransformer.m b/SDWebImage/SDImageTransformer.m index 9cbb0015..26ee45cd 100644 --- a/SDWebImage/SDImageTransformer.m +++ b/SDWebImage/SDImageTransformer.m @@ -19,7 +19,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 SDImagePipelineTransformer () 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 {