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..60fe022f 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,10 @@ return [testBundle pathForResource:@"TestImage" ofType:@"jpg"]; } +#if SD_UIKIT +- (NSString *)testBackgroundImageOperationKeyForState:(UIControlState)state { + return [NSString stringWithFormat:@"UIButtonBackgroundImageOperation%lu", (unsigned long)state]; +} +#endif + @end