Merge pull request #2635 from dreampiggy/bugfix_transformer_cache_key
Fix the current transformer cache key generating rules, try to keep the image file extension
This commit is contained in:
commit
71bff450b3
|
@ -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 ()
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue