From 2396c2e7c048d2e04cf25c39eda11cb355c394e1 Mon Sep 17 00:00:00 2001 From: kinarob Date: Fri, 24 May 2019 21:09:59 +0800 Subject: [PATCH 1/2] Fix get button background image operation key bug --- SDWebImage/UIButton+WebCache.m | 2 +- Tests/Tests/SDImageTransformerTests.m | 11 +++-------- Tests/Tests/SDWebCacheCategoriesTests.m | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/SDWebImage/UIButton+WebCache.m b/SDWebImage/UIButton+WebCache.m index caf3a401..6621cf56 100644 --- a/SDWebImage/UIButton+WebCache.m +++ b/SDWebImage/UIButton+WebCache.m @@ -189,7 +189,7 @@ static inline NSString * backgroundImageOperationKeyForState(UIControlState stat } else { mutableContext = [NSMutableDictionary dictionary]; } - mutableContext[SDWebImageContextSetImageOperationKey] = imageOperationKeyForState(state); + mutableContext[SDWebImageContextSetImageOperationKey] = backgroundImageOperationKeyForState(state); @weakify(self); [self sd_internalSetImageWithURL:url placeholderImage:placeholder diff --git a/Tests/Tests/SDImageTransformerTests.m b/Tests/Tests/SDImageTransformerTests.m index 14a72088..d0105f57 100644 --- a/Tests/Tests/SDImageTransformerTests.m +++ b/Tests/Tests/SDImageTransformerTests.m @@ -171,10 +171,9 @@ SDImageTintTransformer *transformer6 = [SDImageTintTransformer transformerWithColor:tintColor]; SDImageBlurTransformer *transformer7 = [SDImageBlurTransformer transformerWithRadius:blurRadius]; -#if SD_UIKIT || SD_MAC CIFilter *filter = [CIFilter filterWithName:@"CIColorInvert"]; SDImageFilterTransformer *transformer8 = [SDImageFilterTransformer transformerWithFilter:filter]; -#endif + // Chain all built-in transformers for test case SDImagePipelineTransformer *pipelineTransformer = [SDImagePipelineTransformer transformerWithTransformers:@[ transformer1, @@ -184,9 +183,7 @@ transformer5, transformer6, transformer7, -#if SD_UIKIT || SD_MAC - transformer8, -#endif + transformer8 ]]; NSArray *transformerKeys = @[ @"SDImageResizingTransformer({100.000000,100.000000},2)", @@ -196,9 +193,7 @@ @"SDImageCroppingTransformer({0.000000,0.000000,50.000000,50.000000})", @"SDImageTintTransformer(#00000000)", @"SDImageBlurTransformer(5.000000)", -#if SD_UIKIT || SD_MAC - @"SDImageFilterTransformer(CIColorInvert)", -#endif + @"SDImageFilterTransformer(CIColorInvert)" ]; NSString *transformerKey = [transformerKeys componentsJoinedByString:@"-"]; // SDImageTransformerKeySeparator expect([pipelineTransformer.transformerKey isEqualToString:transformerKey]).beTruthy(); diff --git a/Tests/Tests/SDWebCacheCategoriesTests.m b/Tests/Tests/SDWebCacheCategoriesTests.m index fb3cec08..1f841cd7 100644 --- a/Tests/Tests/SDWebCacheCategoriesTests.m +++ b/Tests/Tests/SDWebCacheCategoriesTests.m @@ -120,6 +120,16 @@ }]; [self waitForExpectationsWithCommonTimeout]; } + +- (void)testUIButtonBackgroundImageCancelCurrentImageLoad { + UIButton *button = [[UIButton alloc] init]; + NSURL *originalImageURL = [NSURL URLWithString:kTestJPEGURL]; + [button sd_setBackgroundImageWithURL:originalImageURL forState:UIControlStateNormal]; + [button sd_cancelBackgroundImageLoadForState:UIControlStateNormal]; + NSString *backgroundImageOperationKey = [self testBackgroundImageOperationKeyForState:UIControlStateNormal]; + expect([button sd_imageLoadOperationForKey:backgroundImageOperationKey]).beNil(); +} + #endif #if SD_MAC @@ -339,4 +349,8 @@ return [testBundle pathForResource:@"TestImage" ofType:@"jpg"]; } +- (NSString *)testBackgroundImageOperationKeyForState:(UIControlState)state { + return [NSString stringWithFormat:@"UIButtonBackgroundImageOperation%lu", (unsigned long)state]; +} + @end From 3c2485654140e5aac04d4af9a627bae2127d5179 Mon Sep 17 00:00:00 2001 From: kinarob Date: Sat, 25 May 2019 21:30:22 +0800 Subject: [PATCH 2/2] `backgroundImageOperationKey` test should apply for SD_UIKIT only --- Tests/Tests/SDWebCacheCategoriesTests.m | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Tests/Tests/SDWebCacheCategoriesTests.m b/Tests/Tests/SDWebCacheCategoriesTests.m index 1f841cd7..60fe022f 100644 --- a/Tests/Tests/SDWebCacheCategoriesTests.m +++ b/Tests/Tests/SDWebCacheCategoriesTests.m @@ -349,8 +349,10 @@ return [testBundle pathForResource:@"TestImage" ofType:@"jpg"]; } +#if SD_UIKIT - (NSString *)testBackgroundImageOperationKeyForState:(UIControlState)state { return [NSString stringWithFormat:@"UIButtonBackgroundImageOperation%lu", (unsigned long)state]; } +#endif @end