Fix the current transformer cache key generating rules, try to keep the image file extension as much as we can

This commit is contained in:
DreamPiggy 2019-03-09 17:07:47 +08:00
parent 78e408c537
commit 9fb9ac7ad4
2 changed files with 53 additions and 1 deletions

View File

@ -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)

View File

@ -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 {