Fix the bug that `SDWebImageContextCacheKeyFilter` wrongly be used as cache serializer and cause crash

This commit is contained in:
DreamPiggy 2018-08-17 17:20:17 +08:00
parent 39893fbdac
commit b400b51cca
2 changed files with 38 additions and 4 deletions

View File

@ -283,7 +283,7 @@ static id<SDImageLoader> _defaultImageLoader;
id<SDWebImageCacheKeyFilter> cacheKeyFilter = context[SDWebImageContextCacheKeyFilter];
NSString *key = [self cacheKeyForURL:url cacheKeyFilter:cacheKeyFilter];
id<SDImageTransformer> transformer = context[SDWebImageContextImageTransformer];
id<SDWebImageCacheSerializer> cacheSerializer = context[SDWebImageContextCacheKeyFilter];
id<SDWebImageCacheSerializer> cacheSerializer = context[SDWebImageContextCacheSerializer];
if (downloadedImage && (!downloadedImage.sd_isAnimated || (options & SDWebImageTransformAnimatedImage)) && transformer) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
UIImage *transformedImage = [transformer transformedImageWithImage:downloadedImage forKey:key];

View File

@ -113,9 +113,8 @@
XCTestExpectation *expectation = [self expectationWithDescription:@"Image transformer work"];
NSURL *imageURL = [NSURL URLWithString:kTestJPEGURL];
SDWebImageTestTransformer *transformer = [[SDWebImageTestTransformer alloc] init];
NSBundle *testBundle = [NSBundle bundleForClass:[self class]];
NSString *testImagePath = [testBundle pathForResource:@"TestImage" ofType:@"jpg"];
transformer.testImage = [[UIImage alloc] initWithContentsOfFile:testImagePath];
transformer.testImage = [[UIImage alloc] initWithContentsOfFile:[self testJPEGPath]];
SDWebImageManager *manager = [[SDWebImageManager alloc] initWithCache:[SDImageCache sharedImageCache] loader:[SDWebImageDownloader sharedDownloader]];
manager.transformer = transformer;
[[SDImageCache sharedImageCache] removeImageForKey:kTestJPEGURL withCompletion:^{
@ -128,4 +127,39 @@
[self waitForExpectationsWithCommonTimeout];
}
- (void)test09ThatCacheKeyFilterWork {
XCTestExpectation *expectation = [self expectationWithDescription:@"Cache key filter work"];
NSURL *imageURL = [NSURL URLWithString:kTestJPEGURL];
NSString *cacheKey = @"kTestJPEGURL";
SDWebImageCacheKeyFilter *cacheKeyFilter = [SDWebImageCacheKeyFilter cacheKeyFilterWithBlock:^NSString * _Nullable(NSURL * _Nonnull url) {
if ([url isEqual:imageURL]) {
return cacheKey;
} else {
return url.absoluteString;
}
}];
SDWebImageManager *manager = [[SDWebImageManager alloc] initWithCache:[SDImageCache sharedImageCache] loader:[SDWebImageDownloader sharedDownloader]];
manager.cacheKeyFilter = cacheKeyFilter;
// Check download and retrieve custom cache key
[manager loadImageWithURL:imageURL options:0 context:@{SDWebImageContextStoreCacheType : @(SDImageCacheTypeMemory)} progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
expect(cacheType).equal(SDImageCacheTypeNone);
// Check memory cache exist
[manager.imageCache containsImageForKey:cacheKey cacheType:SDImageCacheTypeMemory completion:^(SDImageCacheType containsCacheType) {
expect(containsCacheType).equal(SDImageCacheTypeMemory);
[expectation fulfill];
}];
}];
[self waitForExpectationsWithCommonTimeout];
}
- (NSString *)testJPEGPath {
NSBundle *testBundle = [NSBundle bundleForClass:[self class]];
return [testBundle pathForResource:@"TestImage" ofType:@"jpg"];
}
@end