From 51024a34f7de1b1bd8d26023de1f8adf2024a07e Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Thu, 8 Mar 2018 12:13:52 +0800 Subject: [PATCH 1/2] Add cacheKeyFilter to allow user provide modified version of data when storing the disk cache in SDWebImageManager --- SDWebImage/SDWebImageManager.h | 28 +++++++++++++++++++++++++--- SDWebImage/SDWebImageManager.m | 17 +++++++++++++++-- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/SDWebImage/SDWebImageManager.h b/SDWebImage/SDWebImageManager.h index 508042ec..3f872028 100644 --- a/SDWebImage/SDWebImageManager.h +++ b/SDWebImage/SDWebImageManager.h @@ -122,7 +122,9 @@ typedef void(^SDExternalCompletionBlock)(UIImage * _Nullable image, NSError * _N typedef void(^SDInternalCompletionBlock)(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL); -typedef NSString * _Nullable (^SDWebImageCacheKeyFilterBlock)(NSURL * _Nullable url); +typedef NSString * _Nullable(^SDWebImageCacheKeyFilterBlock)(NSURL * _Nullable url); + +typedef NSData * _Nullable(^SDWebImageCacheSerializerBlock)(UIImage * _Nonnull image, NSData * _Nullable data, NSURL * _Nullable imageURL); @class SDWebImageManager; @@ -193,15 +195,35 @@ SDWebImageManager *manager = [SDWebImageManager sharedManager]; * * @code -[[SDWebImageManager sharedManager] setCacheKeyFilter:^(NSURL *url) { +SDWebImageManager.sharedManager.cacheKeyFilter = ^(NSURL * _Nullable url) { url = [[NSURL alloc] initWithScheme:url.scheme host:url.host path:url.path]; return [url absoluteString]; -}]; +}; * @endcode */ @property (nonatomic, copy, nullable) SDWebImageCacheKeyFilterBlock cacheKeyFilter; +/** + * The cache serializer is a block used to convert the decoded image, the source downloaded data, to the actual data used for storing to the disk cache. If you return nil, means to generate the data from the image instance, see `SDImageCache`. + * For example, if you are using WebP images and facing the slow decoding time issue when later retriving from disk cache again. You can try to encode the decoded image to JPEG/PNG format to disk cache instead of source downloaded data. + * @note The `image` arg is nonnull, but when you also provide a image transformer and the image is transformed, the `data` arg may be nil, take attention to this case. + * @note This method is called from a global queue in order to not to block the main thread. + * @code + SDWebImageManager.sharedManager.cacheKeyFilter = ^NSData * _Nullable(UIImage * _Nonnull image, NSData * _Nullable data, NSURL * _Nullable imageURL) { + SDImageFormat format = [NSData sd_imageFormatForImageData:data]; + switch (format) { + case SDImageFormatWebP: + return image.images ? data : nil; + default: + return data; + } + }; + * @endcode + * The default value is nil. Means we just store the source downloaded data to disk cache. + */ +@property (nonatomic, copy, nullable) SDWebImageCacheSerializerBlock cacheSerializer; + /** * Returns global SDWebImageManager instance. * diff --git a/SDWebImage/SDWebImageManager.m b/SDWebImage/SDWebImageManager.m index ddf1346d..e03e3cef 100644 --- a/SDWebImage/SDWebImageManager.m +++ b/SDWebImage/SDWebImageManager.m @@ -232,15 +232,28 @@ if (transformedImage && finished) { BOOL imageWasTransformed = ![transformedImage isEqual:downloadedImage]; + NSData *cacheData; // pass nil if the image was transformed, so we can recalculate the data from the image - [self.imageCache storeImage:transformedImage imageData:(imageWasTransformed ? nil : downloadedData) forKey:key toDisk:cacheOnDisk completion:nil]; + if (self.cacheSerializer) { + cacheData = self.cacheSerializer(transformedImage, (imageWasTransformed ? nil : downloadedData), url); + } else { + cacheData = (imageWasTransformed ? nil : downloadedData); + } + [self.imageCache storeImage:transformedImage imageData:cacheData forKey:key toDisk:cacheOnDisk completion:nil]; } [self callCompletionBlockForOperation:strongSubOperation completion:completedBlock image:transformedImage data:downloadedData error:nil cacheType:SDImageCacheTypeNone finished:finished url:url]; }); } else { if (downloadedImage && finished) { - [self.imageCache storeImage:downloadedImage imageData:downloadedData forKey:key toDisk:cacheOnDisk completion:nil]; + if (self.cacheSerializer) { + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ + NSData *cacheData = self.cacheSerializer(downloadedImage, downloadedData, url); + [self.imageCache storeImage:downloadedImage imageData:cacheData forKey:key toDisk:cacheOnDisk completion:nil]; + }); + } else { + [self.imageCache storeImage:downloadedImage imageData:downloadedData forKey:key toDisk:cacheOnDisk completion:nil]; + } } [self callCompletionBlockForOperation:strongSubOperation completion:completedBlock image:downloadedImage data:downloadedData error:nil cacheType:SDImageCacheTypeNone finished:finished url:url]; } From dc5c974b89509992d6c6e0d0510344fd0e35baa6 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Mon, 12 Mar 2018 23:06:02 +0800 Subject: [PATCH 2/2] Bumped version to 4.3.3 update CHANGELOG --- CHANGELOG.md | 13 +++++++++++++ SDWebImage.podspec | 2 +- WebImage/Info.plist | 4 ++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ea9b2ae0..82e062e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,16 @@ +## [4.3.3 - Cache Serializer, on Mar 12th, 2018](https://github.com/rs/SDWebImage/releases/tag/4.3.3) +See [all tickets marked for the 4.3.3 release](https://github.com/rs/SDWebImage/milestone/24) + +#### Features +- Manager + - Add cacheSerializer to allow user provide modified version of data when storing the disk cache in SDWebImageManager #2245 + - Add a delegate method to control the custom logic when blocking the failed url #2246 + +#### Improvements +- Project + - Enable CLANG\_WARN\_OBJC\_IMPLICIT\_RETAIN\_SELF and fix warning #2242 + + ## [4.3.2 - 4.3 Patch, on Feb 28th, 2018](https://github.com/rs/SDWebImage/releases/tag/4.3.2) See [all tickets marked for the 4.3.2 release](https://github.com/rs/SDWebImage/milestone/23) diff --git a/SDWebImage.podspec b/SDWebImage.podspec index b805b502..b56cfe8d 100644 --- a/SDWebImage.podspec +++ b/SDWebImage.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'SDWebImage' - s.version = '4.3.2' + s.version = '4.3.3' s.osx.deployment_target = '10.9' s.ios.deployment_target = '7.0' diff --git a/WebImage/Info.plist b/WebImage/Info.plist index 96379299..1f3f55d0 100644 --- a/WebImage/Info.plist +++ b/WebImage/Info.plist @@ -15,11 +15,11 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 4.3.2 + 4.3.3 CFBundleSignature ???? CFBundleVersion - 4.3.2 + 4.3.3 NSPrincipalClass