diff --git a/Demo/YYWebImageDemo/YYWebImageExample.m b/Demo/YYWebImageDemo/YYWebImageExample.m index bdd7522..34e2cb7 100644 --- a/Demo/YYWebImageDemo/YYWebImageExample.m +++ b/Demo/YYWebImageDemo/YYWebImageExample.m @@ -98,14 +98,15 @@ - (void)setImageURL:(NSURL *)url { } } transform:nil - completion:^(UIImage *image, NSURL *url, YYWebImageFromType from, YYWebImageStage stage, NSError *error) { - if (stage == YYWebImageStageFinished) { - _self.progressLayer.hidden = YES; - [_self.indicator stopAnimating]; - _self.indicator.hidden = YES; - if (!image) _self.label.hidden = NO; - } - }]; + transformKey:nil + completion:^(UIImage *image, NSURL *url, YYWebImageFromType from, YYWebImageStage stage, NSError *error) { + if (stage == YYWebImageStageFinished) { + _self.progressLayer.hidden = YES; + [_self.indicator stopAnimating]; + _self.indicator.hidden = YES; + if (!image) _self.label.hidden = NO; + } + }]; } - (void)prepareForReuse { diff --git a/YYWebImage/Categories/CALayer+YYWebImage.h b/YYWebImage/Categories/CALayer+YYWebImage.h index 722edd8..b79f280 100644 --- a/YYWebImage/Categories/CALayer+YYWebImage.h +++ b/YYWebImage/Categories/CALayer+YYWebImage.h @@ -69,30 +69,33 @@ NS_ASSUME_NONNULL_BEGIN /** Set the view's `image` with a specified URL. - @param imageURL The image url (remote or local file path). - @param placeholder The image to be set initially, until the image request finishes. - @param options The options to use when request the image. - @param progress The block invoked (on main thread) during image request. - @param transform The block invoked (on background thread) to do additional image process. - @param completion The block invoked (on main thread) when image request completed. + @param imageURL The image url (remote or local file path). + @param placeholder The image to be set initially, until the image request finishes. + @param options The options to use when request the image. + @param progress The block invoked (on main thread) during image request. + @param transform The block invoked (on background thread) to do additional image process. + @param transformKey 转换key,链接相同但key不同会有不同的缓存 + @param completion The block invoked (on main thread) when image request completed. */ - (void)yy_setImageWithURL:(nullable NSURL *)imageURL placeholder:(nullable UIImage *)placeholder options:(YYWebImageOptions)options progress:(nullable YYWebImageProgressBlock)progress transform:(nullable YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(nullable YYWebImageCompletionBlock)completion; /** Set the view's `image` with a specified URL. - @param imageURL The image url (remote or local file path). - @param placeholder he image to be set initially, until the image request finishes. - @param options The options to use when request the image. - @param manager The manager to create image request operation. - @param progress The block invoked (on main thread) during image request. - @param transform The block invoked (on background thread) to do additional image process. - @param completion The block invoked (on main thread) when image request completed. + @param imageURL The image url (remote or local file path). + @param placeholder he image to be set initially, until the image request finishes. + @param options The options to use when request the image. + @param manager The manager to create image request operation. + @param progress The block invoked (on main thread) during image request. + @param transform The block invoked (on background thread) to do additional image process. + @param transformKey 转换key,链接相同但key不同会有不同的缓存 + @param completion The block invoked (on main thread) when image request completed. */ - (void)yy_setImageWithURL:(nullable NSURL *)imageURL placeholder:(nullable UIImage *)placeholder @@ -100,6 +103,7 @@ NS_ASSUME_NONNULL_BEGIN manager:(nullable YYWebImageManager *)manager progress:(nullable YYWebImageProgressBlock)progress transform:(nullable YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(nullable YYWebImageCompletionBlock)completion; /** diff --git a/YYWebImage/Categories/CALayer+YYWebImage.m b/YYWebImage/Categories/CALayer+YYWebImage.m index 8309a0c..620e200 100644 --- a/YYWebImage/Categories/CALayer+YYWebImage.m +++ b/YYWebImage/Categories/CALayer+YYWebImage.m @@ -35,6 +35,7 @@ - (void)setYy_imageURL:(NSURL *)imageURL { manager:nil progress:nil transform:nil + transformKey:nil completion:nil]; } @@ -45,6 +46,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL placeholder:(UIImage *)placeholder manager:nil progress:nil transform:nil + transformKey:nil completion:nil]; } @@ -55,6 +57,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL options:(YYWebImageOptions)options manager:nil progress:nil transform:nil + transformKey:nil completion:nil]; } @@ -68,6 +71,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL manager:nil progress:nil transform:nil + transformKey:nil completion:completion]; } @@ -76,6 +80,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL options:(YYWebImageOptions)options progress:(YYWebImageProgressBlock)progress transform:(YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(YYWebImageCompletionBlock)completion { [self yy_setImageWithURL:imageURL placeholder:placeholder @@ -83,6 +88,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL manager:nil progress:progress transform:transform + transformKey:transformKey completion:completion]; } @@ -92,6 +98,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL manager:(YYWebImageManager *)manager progress:(YYWebImageProgressBlock)progress transform:(YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(YYWebImageCompletionBlock)completion { if ([imageURL isKindOfClass:[NSString class]]) imageURL = [NSURL URLWithString:(id)imageURL]; manager = manager ? manager : [YYWebImageManager sharedManager]; @@ -122,7 +129,8 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL if (manager.cache && !(options & YYWebImageOptionUseNSURLCache) && !(options & YYWebImageOptionRefreshImageCache)) { - imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL] withType:YYImageCacheTypeMemory]; + imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL transformKey:transformKey +] withType:YYImageCacheTypeMemory]; } if (imageFromMemory) { if (!(options & YYWebImageOptionAvoidSetImage)) { @@ -173,7 +181,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL }); }; - newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transform:transform completion:_completion]; + newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transform:transform transformKey:transformKey completion:_completion]; weakSetter = setter; }); diff --git a/YYWebImage/Categories/MKAnnotationView+YYWebImage.h b/YYWebImage/Categories/MKAnnotationView+YYWebImage.h index b9bce0b..ec8803d 100644 --- a/YYWebImage/Categories/MKAnnotationView+YYWebImage.h +++ b/YYWebImage/Categories/MKAnnotationView+YYWebImage.h @@ -66,30 +66,33 @@ NS_ASSUME_NONNULL_BEGIN /** Set the view's `image` with a specified URL. - @param imageURL The image url (remote or local file path). - @param placeholder The image to be set initially, until the image request finishes. - @param options The options to use when request the image. - @param progress The block invoked (on main thread) during image request. - @param transform The block invoked (on background thread) to do additional image process. - @param completion The block invoked (on main thread) when image request completed. + @param imageURL The image url (remote or local file path). + @param placeholder The image to be set initially, until the image request finishes. + @param options The options to use when request the image. + @param progress The block invoked (on main thread) during image request. + @param transform The block invoked (on background thread) to do additional image process. + @param transformKey 转换key,链接相同但key不同会有不同的缓存 + @param completion The block invoked (on main thread) when image request completed. */ - (void)yy_setImageWithURL:(nullable NSURL *)imageURL placeholder:(nullable UIImage *)placeholder options:(YYWebImageOptions)options progress:(nullable YYWebImageProgressBlock)progress transform:(nullable YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(nullable YYWebImageCompletionBlock)completion; /** Set the view's `image` with a specified URL. - @param imageURL The image url (remote or local file path). - @param placeholder he image to be set initially, until the image request finishes. - @param options The options to use when request the image. - @param manager The manager to create image request operation. - @param progress The block invoked (on main thread) during image request. - @param transform The block invoked (on background thread) to do additional image process. - @param completion The block invoked (on main thread) when image request completed. + @param imageURL The image url (remote or local file path). + @param placeholder he image to be set initially, until the image request finishes. + @param options The options to use when request the image. + @param manager The manager to create image request operation. + @param progress The block invoked (on main thread) during image request. + @param transform The block invoked (on background thread) to do additional image process. + @param transformKey 转换key,链接相同但key不同会有不同的缓存 + @param completion The block invoked (on main thread) when image request completed. */ - (void)yy_setImageWithURL:(nullable NSURL *)imageURL placeholder:(nullable UIImage *)placeholder @@ -97,6 +100,7 @@ NS_ASSUME_NONNULL_BEGIN manager:(nullable YYWebImageManager *)manager progress:(nullable YYWebImageProgressBlock)progress transform:(nullable YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(nullable YYWebImageCompletionBlock)completion; /** diff --git a/YYWebImage/Categories/MKAnnotationView+YYWebImage.m b/YYWebImage/Categories/MKAnnotationView+YYWebImage.m index d9afff4..d213613 100644 --- a/YYWebImage/Categories/MKAnnotationView+YYWebImage.m +++ b/YYWebImage/Categories/MKAnnotationView+YYWebImage.m @@ -35,6 +35,7 @@ - (void)setYy_imageURL:(NSURL *)imageURL { manager:nil progress:nil transform:nil + transformKey:nil completion:nil]; } @@ -45,6 +46,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL placeholder:(UIImage *)placeholder manager:nil progress:nil transform:nil + transformKey:nil completion:nil]; } @@ -55,6 +57,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL options:(YYWebImageOptions)options manager:nil progress:nil transform:nil + transformKey:nil completion:nil]; } @@ -68,6 +71,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL manager:nil progress:nil transform:nil + transformKey:nil completion:completion]; } @@ -76,6 +80,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL options:(YYWebImageOptions)options progress:(YYWebImageProgressBlock)progress transform:(YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(YYWebImageCompletionBlock)completion { [self yy_setImageWithURL:imageURL placeholder:placeholder @@ -83,6 +88,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL manager:nil progress:progress transform:transform + transformKey:transformKey completion:completion]; } @@ -92,6 +98,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL manager:(YYWebImageManager *)manager progress:(YYWebImageProgressBlock)progress transform:(YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(YYWebImageCompletionBlock)completion { if ([imageURL isKindOfClass:[NSString class]]) imageURL = [NSURL URLWithString:(id)imageURL]; manager = manager ? manager : [YYWebImageManager sharedManager]; @@ -122,7 +129,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL if (manager.cache && !(options & YYWebImageOptionUseNSURLCache) && !(options & YYWebImageOptionRefreshImageCache)) { - imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL] withType:YYImageCacheTypeMemory]; + imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL transformKey:transformKey] withType:YYImageCacheTypeMemory]; } if (imageFromMemory) { if (!(options & YYWebImageOptionAvoidSetImage)) { @@ -173,7 +180,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL }); }; - newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transform:transform completion:_completion]; + newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transform:transform transformKey:transformKey completion:_completion]; weakSetter = setter; }); }); diff --git a/YYWebImage/Categories/UIButton+YYWebImage.h b/YYWebImage/Categories/UIButton+YYWebImage.h index 7ad6eff..e39b066 100644 --- a/YYWebImage/Categories/UIButton+YYWebImage.h +++ b/YYWebImage/Categories/UIButton+YYWebImage.h @@ -72,13 +72,14 @@ NS_ASSUME_NONNULL_BEGIN /** Set the button's image with a specified URL for the specified state. - @param imageURL The image url (remote or local file path). - @param state The state that uses the specified image. - @param placeholder The image to be set initially, until the image request finishes. - @param options The options to use when request the image. - @param progress The block invoked (on main thread) during image request. - @param transform The block invoked (on background thread) to do additional image process. - @param completion The block invoked (on main thread) when image request completed. + @param imageURL The image url (remote or local file path). + @param state The state that uses the specified image. + @param placeholder The image to be set initially, until the image request finishes. + @param options The options to use when request the image. + @param progress The block invoked (on main thread) during image request. + @param transform The block invoked (on background thread) to do additional image process. + @param transformKey 转换key,链接相同但key不同会有不同的缓存 + @param completion The block invoked (on main thread) when image request completed. */ - (void)yy_setImageWithURL:(nullable NSURL *)imageURL forState:(UIControlState)state @@ -86,19 +87,21 @@ NS_ASSUME_NONNULL_BEGIN options:(YYWebImageOptions)options progress:(nullable YYWebImageProgressBlock)progress transform:(nullable YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(nullable YYWebImageCompletionBlock)completion; /** Set the button's image with a specified URL for the specified state. - @param imageURL The image url (remote or local file path). - @param state The state that uses the specified image. - @param placeholder The image to be set initially, until the image request finishes. - @param options The options to use when request the image. - @param manager The manager to create image request operation. - @param progress The block invoked (on main thread) during image request. - @param transform The block invoked (on background thread) to do additional image process. - @param completion The block invoked (on main thread) when image request completed. + @param imageURL The image url (remote or local file path). + @param state The state that uses the specified image. + @param placeholder The image to be set initially, until the image request finishes. + @param options The options to use when request the image. + @param manager The manager to create image request operation. + @param progress The block invoked (on main thread) during image request. + @param transform The block invoked (on background thread) to do additional image process. + @param transformKey 转换key,链接相同但key不同会有不同的缓存 + @param completion The block invoked (on main thread) when image request completed. */ - (void)yy_setImageWithURL:(nullable NSURL *)imageURL forState:(UIControlState)state @@ -107,6 +110,7 @@ NS_ASSUME_NONNULL_BEGIN manager:(nullable YYWebImageManager *)manager progress:(nullable YYWebImageProgressBlock)progress transform:(nullable YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(nullable YYWebImageCompletionBlock)completion; /** @@ -171,6 +175,7 @@ NS_ASSUME_NONNULL_BEGIN @param options The options to use when request the image. @param progress The block invoked (on main thread) during image request. @param transform The block invoked (on background thread) to do additional image process. + @param transformKey 转换key,链接相同但key不同会有不同的缓存 @param completion The block invoked (on main thread) when image request completed. */ - (void)yy_setBackgroundImageWithURL:(nullable NSURL *)imageURL @@ -179,19 +184,21 @@ NS_ASSUME_NONNULL_BEGIN options:(YYWebImageOptions)options progress:(nullable YYWebImageProgressBlock)progress transform:(nullable YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(nullable YYWebImageCompletionBlock)completion; /** Set the button's backgroundImage with a specified URL for the specified state. - @param imageURL The image url (remote or local file path). - @param state The state that uses the specified image. - @param placeholder The image to be set initially, until the image request finishes. - @param options The options to use when request the image. - @param manager The manager to create image request operation. - @param progress The block invoked (on main thread) during image request. - @param transform The block invoked (on background thread) to do additional image process. - @param completion The block invoked (on main thread) when image request completed. + @param imageURL The image url (remote or local file path). + @param state The state that uses the specified image. + @param placeholder The image to be set initially, until the image request finishes. + @param options The options to use when request the image. + @param manager The manager to create image request operation. + @param progress The block invoked (on main thread) during image request. + @param transform The block invoked (on background thread) to do additional image process. + @param transformKey 转换key,链接相同但key不同会有不同的缓存 + @param completion The block invoked (on main thread) when image request completed. */ - (void)yy_setBackgroundImageWithURL:(nullable NSURL *)imageURL forState:(UIControlState)state @@ -200,6 +207,7 @@ NS_ASSUME_NONNULL_BEGIN manager:(nullable YYWebImageManager *)manager progress:(nullable YYWebImageProgressBlock)progress transform:(nullable YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(nullable YYWebImageCompletionBlock)completion; /** diff --git a/YYWebImage/Categories/UIButton+YYWebImage.m b/YYWebImage/Categories/UIButton+YYWebImage.m index 8cc21d5..ea795a3 100644 --- a/YYWebImage/Categories/UIButton+YYWebImage.m +++ b/YYWebImage/Categories/UIButton+YYWebImage.m @@ -84,6 +84,7 @@ - (void)_yy_setImageWithURL:(NSURL *)imageURL manager:(YYWebImageManager *)manager progress:(YYWebImageProgressBlock)progress transform:(YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(YYWebImageCompletionBlock)completion { if ([imageURL isKindOfClass:[NSString class]]) imageURL = [NSURL URLWithString:(id)imageURL]; manager = manager ? manager : [YYWebImageManager sharedManager]; @@ -109,7 +110,7 @@ - (void)_yy_setImageWithURL:(NSURL *)imageURL if (manager.cache && !(options & YYWebImageOptionUseNSURLCache) && !(options & YYWebImageOptionRefreshImageCache)) { - imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL] withType:YYImageCacheTypeMemory]; + imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL transformKey:transformKey] withType:YYImageCacheTypeMemory]; } if (imageFromMemory) { if (!(options & YYWebImageOptionAvoidSetImage)) { @@ -153,7 +154,7 @@ - (void)_yy_setImageWithURL:(NSURL *)imageURL }); }; - newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transform:transform completion:_completion]; + newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transform:transform transformKey:transformKey completion:_completion]; weakSetter = setter; }); }); @@ -181,6 +182,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL manager:nil progress:nil transform:nil + transformKey:nil completion:nil]; } @@ -194,6 +196,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL manager:nil progress:nil transform:nil + transformKey:nil completion:nil]; } @@ -209,6 +212,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL manager:nil progress:nil transform:nil + transformKey:nil completion:completion]; } @@ -218,6 +222,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL options:(YYWebImageOptions)options progress:(YYWebImageProgressBlock)progress transform:(YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(YYWebImageCompletionBlock)completion { [self yy_setImageWithURL:imageURL forState:state @@ -226,6 +231,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL manager:nil progress:progress transform:transform + transformKey:transformKey completion:completion]; } @@ -236,6 +242,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL manager:(YYWebImageManager *)manager progress:(YYWebImageProgressBlock)progress transform:(YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(YYWebImageCompletionBlock)completion { for (NSNumber *num in UIControlStateMulti(state)) { [self _yy_setImageWithURL:imageURL @@ -245,6 +252,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL manager:manager progress:progress transform:transform + transformKey:transformKey completion:completion]; } } @@ -265,6 +273,7 @@ - (void)_yy_setBackgroundImageWithURL:(NSURL *)imageURL manager:(YYWebImageManager *)manager progress:(YYWebImageProgressBlock)progress transform:(YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(YYWebImageCompletionBlock)completion { if ([imageURL isKindOfClass:[NSString class]]) imageURL = [NSURL URLWithString:(id)imageURL]; manager = manager ? manager : [YYWebImageManager sharedManager]; @@ -290,7 +299,7 @@ - (void)_yy_setBackgroundImageWithURL:(NSURL *)imageURL if (manager.cache && !(options & YYWebImageOptionUseNSURLCache) && !(options & YYWebImageOptionRefreshImageCache)) { - imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL] withType:YYImageCacheTypeMemory]; + imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL transformKey:transformKey] withType:YYImageCacheTypeMemory]; } if (imageFromMemory) { if (!(options & YYWebImageOptionAvoidSetImage)) { @@ -334,7 +343,7 @@ - (void)_yy_setBackgroundImageWithURL:(NSURL *)imageURL }); }; - newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transform:transform completion:_completion]; + newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transform:transform transformKey:transformKey completion:_completion]; weakSetter = setter; }); }); @@ -362,6 +371,7 @@ - (void)yy_setBackgroundImageWithURL:(NSURL *)imageURL manager:nil progress:nil transform:nil + transformKey:nil completion:nil]; } @@ -375,6 +385,7 @@ - (void)yy_setBackgroundImageWithURL:(NSURL *)imageURL manager:nil progress:nil transform:nil + transformKey:nil completion:nil]; } @@ -390,6 +401,7 @@ - (void)yy_setBackgroundImageWithURL:(NSURL *)imageURL manager:nil progress:nil transform:nil + transformKey:nil completion:completion]; } @@ -399,6 +411,7 @@ - (void)yy_setBackgroundImageWithURL:(NSURL *)imageURL options:(YYWebImageOptions)options progress:(YYWebImageProgressBlock)progress transform:(YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(YYWebImageCompletionBlock)completion { [self yy_setBackgroundImageWithURL:imageURL forState:state @@ -407,6 +420,7 @@ - (void)yy_setBackgroundImageWithURL:(NSURL *)imageURL manager:nil progress:progress transform:transform + transformKey:transformKey completion:completion]; } @@ -417,6 +431,7 @@ - (void)yy_setBackgroundImageWithURL:(NSURL *)imageURL manager:(YYWebImageManager *)manager progress:(YYWebImageProgressBlock)progress transform:(YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(YYWebImageCompletionBlock)completion { for (NSNumber *num in UIControlStateMulti(state)) { [self _yy_setBackgroundImageWithURL:imageURL @@ -426,6 +441,7 @@ - (void)yy_setBackgroundImageWithURL:(NSURL *)imageURL manager:manager progress:progress transform:transform + transformKey:transformKey completion:completion]; } } diff --git a/YYWebImage/Categories/UIImageView+YYWebImage.h b/YYWebImage/Categories/UIImageView+YYWebImage.h index 6a66233..b232fb0 100644 --- a/YYWebImage/Categories/UIImageView+YYWebImage.h +++ b/YYWebImage/Categories/UIImageView+YYWebImage.h @@ -67,30 +67,33 @@ NS_ASSUME_NONNULL_BEGIN /** Set the view's `image` with a specified URL. - @param imageURL The image url (remote or local file path). - @param placeholder The image to be set initially, until the image request finishes. - @param options The options to use when request the image. - @param progress The block invoked (on main thread) during image request. - @param transform The block invoked (on background thread) to do additional image process. - @param completion The block invoked (on main thread) when image request completed. + @param imageURL The image url (remote or local file path). + @param placeholder The image to be set initially, until the image request finishes. + @param options The options to use when request the image. + @param progress The block invoked (on main thread) during image request. + @param transform The block invoked (on background thread) to do additional image process. + @param transformKey 转换key,链接相同但key不同会有不同的缓存 + @param completion The block invoked (on main thread) when image request completed. */ - (void)yy_setImageWithURL:(nullable NSURL *)imageURL placeholder:(nullable UIImage *)placeholder options:(YYWebImageOptions)options progress:(nullable YYWebImageProgressBlock)progress transform:(nullable YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(nullable YYWebImageCompletionBlock)completion; /** Set the view's `image` with a specified URL. - @param imageURL The image url (remote or local file path). - @param placeholder he image to be set initially, until the image request finishes. - @param options The options to use when request the image. - @param manager The manager to create image request operation. - @param progress The block invoked (on main thread) during image request. - @param transform The block invoked (on background thread) to do additional image process. - @param completion The block invoked (on main thread) when image request completed. + @param imageURL The image url (remote or local file path). + @param placeholder he image to be set initially, until the image request finishes. + @param options The options to use when request the image. + @param manager The manager to create image request operation. + @param progress The block invoked (on main thread) during image request. + @param transform The block invoked (on background thread) to do additional image process. + @param transformKey 转换key,链接相同但key不同会有不同的缓存 + @param completion The block invoked (on main thread) when image request completed. */ - (void)yy_setImageWithURL:(nullable NSURL *)imageURL placeholder:(nullable UIImage *)placeholder @@ -98,6 +101,7 @@ NS_ASSUME_NONNULL_BEGIN manager:(nullable YYWebImageManager *)manager progress:(nullable YYWebImageProgressBlock)progress transform:(nullable YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(nullable YYWebImageCompletionBlock)completion; /** @@ -155,6 +159,7 @@ NS_ASSUME_NONNULL_BEGIN @param options The options to use when request the image. @param progress The block invoked (on main thread) during image request. @param transform The block invoked (on background thread) to do additional image process. + @param transformKey 转换key,链接相同但key不同会有不同的缓存 @param completion The block invoked (on main thread) when image request completed. */ - (void)yy_setHighlightedImageWithURL:(nullable NSURL *)imageURL @@ -162,6 +167,7 @@ NS_ASSUME_NONNULL_BEGIN options:(YYWebImageOptions)options progress:(nullable YYWebImageProgressBlock)progress transform:(nullable YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(nullable YYWebImageCompletionBlock)completion; /** @@ -173,6 +179,7 @@ NS_ASSUME_NONNULL_BEGIN @param manager The manager to create image request operation. @param progress The block invoked (on main thread) during image request. @param transform The block invoked (on background thread) to do additional image process. + @param transformKey 转换key,链接相同但key不同会有不同的缓存 @param completion The block invoked (on main thread) when image request completed. */ - (void)yy_setHighlightedImageWithURL:(nullable NSURL *)imageURL @@ -181,6 +188,7 @@ NS_ASSUME_NONNULL_BEGIN manager:(nullable YYWebImageManager *)manager progress:(nullable YYWebImageProgressBlock)progress transform:(nullable YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(nullable YYWebImageCompletionBlock)completion; /** diff --git a/YYWebImage/Categories/UIImageView+YYWebImage.m b/YYWebImage/Categories/UIImageView+YYWebImage.m index 1719a12..1dd1c6f 100644 --- a/YYWebImage/Categories/UIImageView+YYWebImage.m +++ b/YYWebImage/Categories/UIImageView+YYWebImage.m @@ -38,6 +38,7 @@ - (void)setYy_imageURL:(NSURL *)imageURL { manager:nil progress:nil transform:nil + transformKey:nil completion:nil]; } @@ -48,6 +49,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL placeholder:(UIImage *)placeholder manager:nil progress:nil transform:nil + transformKey:nil completion:nil]; } @@ -58,6 +60,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL options:(YYWebImageOptions)options manager:nil progress:nil transform:nil + transformKey:nil completion:nil]; } @@ -71,6 +74,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL manager:nil progress:nil transform:nil + transformKey:nil completion:completion]; } @@ -79,6 +83,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL options:(YYWebImageOptions)options progress:(YYWebImageProgressBlock)progress transform:(YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(YYWebImageCompletionBlock)completion { [self yy_setImageWithURL:imageURL placeholder:placeholder @@ -86,6 +91,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL manager:nil progress:progress transform:transform + transformKey:transformKey completion:completion]; } @@ -95,6 +101,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL manager:(YYWebImageManager *)manager progress:(YYWebImageProgressBlock)progress transform:(YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(YYWebImageCompletionBlock)completion { if ([imageURL isKindOfClass:[NSString class]]) imageURL = [NSURL URLWithString:(id)imageURL]; manager = manager ? manager : [YYWebImageManager sharedManager]; @@ -126,7 +133,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL if (manager.cache && !(options & YYWebImageOptionUseNSURLCache) && !(options & YYWebImageOptionRefreshImageCache)) { - imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL] withType:YYImageCacheTypeMemory]; + imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL transformKey:transformKey] withType:YYImageCacheTypeMemory]; } if (imageFromMemory) { if (!(options & YYWebImageOptionAvoidSetImage)) { @@ -177,7 +184,7 @@ - (void)yy_setImageWithURL:(NSURL *)imageURL }); }; - newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transform:transform completion:_completion]; + newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transform:transform transformKey:transformKey completion:_completion]; weakSetter = setter; }); }); @@ -203,6 +210,7 @@ - (void)setYy_highlightedImageURL:(NSURL *)imageURL { manager:nil progress:nil transform:nil + transformKey:nil completion:nil]; } @@ -213,6 +221,7 @@ - (void)yy_setHighlightedImageWithURL:(NSURL *)imageURL placeholder:(UIImage *)p manager:nil progress:nil transform:nil + transformKey:nil completion:nil]; } @@ -223,6 +232,7 @@ - (void)yy_setHighlightedImageWithURL:(NSURL *)imageURL options:(YYWebImageOptio manager:nil progress:nil transform:nil + transformKey:nil completion:nil]; } @@ -236,6 +246,7 @@ - (void)yy_setHighlightedImageWithURL:(NSURL *)imageURL manager:nil progress:nil transform:nil + transformKey:nil completion:completion]; } @@ -244,6 +255,7 @@ - (void)yy_setHighlightedImageWithURL:(NSURL *)imageURL options:(YYWebImageOptions)options progress:(YYWebImageProgressBlock)progress transform:(YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(YYWebImageCompletionBlock)completion { [self yy_setHighlightedImageWithURL:imageURL placeholder:placeholder @@ -251,6 +263,7 @@ - (void)yy_setHighlightedImageWithURL:(NSURL *)imageURL manager:nil progress:progress transform:nil + transformKey:transformKey completion:completion]; } @@ -260,6 +273,7 @@ - (void)yy_setHighlightedImageWithURL:(NSURL *)imageURL manager:(YYWebImageManager *)manager progress:(YYWebImageProgressBlock)progress transform:(YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(YYWebImageCompletionBlock)completion { if ([imageURL isKindOfClass:[NSString class]]) imageURL = [NSURL URLWithString:(id)imageURL]; manager = manager ? manager : [YYWebImageManager sharedManager]; @@ -290,7 +304,7 @@ - (void)yy_setHighlightedImageWithURL:(NSURL *)imageURL if (manager.cache && !(options & YYWebImageOptionUseNSURLCache) && !(options & YYWebImageOptionRefreshImageCache)) { - imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL] withType:YYImageCacheTypeMemory]; + imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL transformKey:transformKey] withType:YYImageCacheTypeMemory]; } if (imageFromMemory) { if (!(options & YYWebImageOptionAvoidSetImage)) { @@ -341,7 +355,7 @@ - (void)yy_setHighlightedImageWithURL:(NSURL *)imageURL }); }; - newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transform:transform completion:_completion]; + newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transform:transform transformKey:transformKey completion:_completion]; weakSetter = setter; }); }); diff --git a/YYWebImage/Categories/_YYWebImageSetter.h b/YYWebImage/Categories/_YYWebImageSetter.h index cf5a118..cd1e2c4 100644 --- a/YYWebImage/Categories/_YYWebImageSetter.h +++ b/YYWebImage/Categories/_YYWebImageSetter.h @@ -51,6 +51,7 @@ extern const NSTimeInterval _YYWebImageProgressiveFadeTime; manager:(YYWebImageManager *)manager progress:(nullable YYWebImageProgressBlock)progress transform:(nullable YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(nullable YYWebImageCompletionBlock)completion; /// Cancel and return a sentinel value. The imageURL will be set to nil. diff --git a/YYWebImage/Categories/_YYWebImageSetter.m b/YYWebImage/Categories/_YYWebImageSetter.m index ec3c99b..6d34591 100644 --- a/YYWebImage/Categories/_YYWebImageSetter.m +++ b/YYWebImage/Categories/_YYWebImageSetter.m @@ -49,13 +49,14 @@ - (int32_t)setOperationWithSentinel:(int32_t)sentinel manager:(YYWebImageManager *)manager progress:(YYWebImageProgressBlock)progress transform:(YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(YYWebImageCompletionBlock)completion { if (sentinel != _sentinel) { if (completion) completion(nil, imageURL, YYWebImageFromNone, YYWebImageStageCancelled, nil); return _sentinel; } - NSOperation *operation = [manager requestImageWithURL:imageURL options:options progress:progress transform:transform completion:completion]; + NSOperation *operation = [manager requestImageWithURL:imageURL options:options progress:progress transform:transform transformKey:transformKey completion:completion]; if (!operation && completion) { NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : @"YYWebImageOperation create failed." }; completion(nil, imageURL, YYWebImageFromNone, YYWebImageStageFinished, [NSError errorWithDomain:@"com.ibireme.webimage" code:-1 userInfo:userInfo]); diff --git a/YYWebImage/YYWebImageManager.h b/YYWebImage/YYWebImageManager.h index 6947faf..915de4d 100644 --- a/YYWebImage/YYWebImageManager.h +++ b/YYWebImage/YYWebImageManager.h @@ -188,17 +188,19 @@ typedef void (^YYWebImageCompletionBlock)(UIImage * _Nullable image, /** Creates and returns a new image operation, the operation will start immediately. - @param url The image url (remote or local file path). - @param options The options to control image operation. - @param progress Progress block which will be invoked on background thread (pass nil to avoid). - @param transform Transform block which will be invoked on background thread (pass nil to avoid). - @param completion Completion block which will be invoked on background thread (pass nil to avoid). + @param url The image url (remote or local file path). + @param options The options to control image operation. + @param progress Progress block which will be invoked on background thread (pass nil to avoid). + @param transform Transform block which will be invoked on background thread (pass nil to avoid). + @param transformKey 转换key,链接相同但key不同会有不同的缓存 + @param completion Completion block which will be invoked on background thread (pass nil to avoid). @return A new image operation. */ - (nullable YYWebImageOperation *)requestImageWithURL:(NSURL *)url options:(YYWebImageOptions)options progress:(nullable YYWebImageProgressBlock)progress transform:(nullable YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(nullable YYWebImageCompletionBlock)completion; /** @@ -273,7 +275,7 @@ typedef void (^YYWebImageCompletionBlock)(UIImage * _Nullable image, @param url A specified URL @return Cache key used in YYImageCache. */ -- (NSString *)cacheKeyForURL:(NSURL *)url; +- (NSString *)cacheKeyForURL:(NSURL *)url transformKey:(nullable NSString *)transformKey; /** diff --git a/YYWebImage/YYWebImageManager.m b/YYWebImage/YYWebImageManager.m index 784fd8b..18d33cd 100644 --- a/YYWebImage/YYWebImageManager.m +++ b/YYWebImage/YYWebImageManager.m @@ -80,6 +80,7 @@ - (YYWebImageOperation *)requestImageWithURL:(NSURL *)url options:(YYWebImageOptions)options progress:(YYWebImageProgressBlock)progress transform:(YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(YYWebImageCompletionBlock)completion { NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; @@ -90,13 +91,14 @@ - (YYWebImageOperation *)requestImageWithURL:(NSURL *)url request.cachePolicy = (options & YYWebImageOptionUseNSURLCache) ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData; - YYWebImageOperation *operation = [[YYWebImageOperation alloc] initWithRequest:request - options:options - cache:_cache - cacheKey:[self cacheKeyForURL:url] - progress:progress - transform:transform ? transform : _sharedTransformBlock - completion:completion]; + YYWebImageOperation *operation = [[YYWebImageOperation alloc]initWithRequest:request + options:options + cache:_cache + cacheKey:[self cacheKeyForURL:url transformKey:transformKey] + progress:progress + transform:transform ? transform : _sharedTransformBlock + transformKey:transformKey + completion:completion]; if (_username && _password) { operation.credential = [NSURLCredential credentialWithUser:_username password:_password persistence:NSURLCredentialPersistenceForSession]; @@ -117,13 +119,15 @@ - (NSDictionary *)headersForURL:(NSURL *)url { return _headersFilter ? _headersFilter(url, _headers) : _headers; } -- (NSString *)cacheKeyForURL:(NSURL *)url { +- (NSString *)cacheKeyForURL:(NSURL *)url transformKey:(nullable NSString *)transformKey { if (!url) return nil; - return _cacheKeyFilter ? _cacheKeyFilter(url) : url.absoluteString; + NSString *cacheKey = _cacheKeyFilter ? _cacheKeyFilter(url) : url.absoluteString; + if (transformKey) + return [NSString stringWithFormat:@"%@--%@", cacheKey, transformKey]; + else + return cacheKey; } - - #pragma mark Network Indicator + (_YYWebImageApplicationNetworkIndicatorInfo *)_networkIndicatorInfo { diff --git a/YYWebImage/YYWebImageOperation.h b/YYWebImage/YYWebImageOperation.h index d15eb3b..67ad62d 100644 --- a/YYWebImage/YYWebImageOperation.h +++ b/YYWebImage/YYWebImageOperation.h @@ -78,7 +78,8 @@ NS_ASSUME_NONNULL_BEGIN The block will be invoked in background thread. Pass nil to avoid it. @param completion A block invoked when image fetch finished or cancelled. The block will be invoked in background thread. Pass nil to avoid it. - + @param transformKey 转换key,链接相同但key不同会有不同的缓存 + @return The image request opeartion, or nil if an error occurs. */ - (instancetype)initWithRequest:(NSURLRequest *)request @@ -87,6 +88,7 @@ NS_ASSUME_NONNULL_BEGIN cacheKey:(nullable NSString *)cacheKey progress:(nullable YYWebImageProgressBlock)progress transform:(nullable YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(nullable YYWebImageCompletionBlock)completion NS_DESIGNATED_INITIALIZER; - (instancetype)init UNAVAILABLE_ATTRIBUTE; diff --git a/YYWebImage/YYWebImageOperation.m b/YYWebImage/YYWebImageOperation.m index 118dd6d..5c38542 100644 --- a/YYWebImage/YYWebImageOperation.m +++ b/YYWebImage/YYWebImageOperation.m @@ -178,6 +178,7 @@ @interface YYWebImageOperation() @property (nonatomic, copy) YYWebImageProgressBlock progress; @property (nonatomic, copy) YYWebImageTransformBlock transform; +@property (nonatomic, copy) NSString *transformKey; @property (nonatomic, copy) YYWebImageCompletionBlock completion; @end @@ -241,7 +242,7 @@ + (dispatch_queue_t)_imageQueue { - (instancetype)init { @throw [NSException exceptionWithName:@"YYWebImageOperation init error" reason:@"YYWebImageOperation must be initialized with a request. Use the designated initializer to init." userInfo:nil]; - return [self initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@""]] options:0 cache:nil cacheKey:nil progress:nil transform:nil completion:nil]; + return [self initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@""]] options:0 cache:nil cacheKey:nil progress:nil transform:nil transformKey:nil completion:nil]; } - (instancetype)initWithRequest:(NSURLRequest *)request @@ -250,6 +251,7 @@ - (instancetype)initWithRequest:(NSURLRequest *)request cacheKey:(NSString *)cacheKey progress:(YYWebImageProgressBlock)progress transform:(YYWebImageTransformBlock)transform + transformKey:(nullable NSString *)transformKey completion:(YYWebImageCompletionBlock)completion { self = [super init]; if (!self) return nil; @@ -261,6 +263,7 @@ - (instancetype)initWithRequest:(NSURLRequest *)request _shouldUseCredentialStorage = YES; _progress = progress; _transform = transform; + _transformKey = transformKey; _completion = completion; _executing = NO; _finished = NO;