The `storeImage:` methods from `SDImageCache` were async already, but declared a…
…s sync. Properly marked them as async + added completion. Got rid of the recalculate param. If the `NSData` is provided, use it. Otherwise, recalculate from the `UIImage`
Showing
3 changed files
with
56 additions
and
19 deletions
@@ -93,35 +93,48 @@ typedef void(^SDWebImageCalculateSizeBlock)(NSUInteger fileCount, NSUInteger tot | @@ -93,35 +93,48 @@ typedef void(^SDWebImageCalculateSizeBlock)(NSUInteger fileCount, NSUInteger tot | ||
93 | */ | 93 | */ |
94 | - (void)addReadOnlyCachePath:(nonnull NSString *)path; | 94 | - (void)addReadOnlyCachePath:(nonnull NSString *)path; |
95 | 95 | ||
96 | +#pragma mark - Store Ops | ||
97 | + | ||
96 | /** | 98 | /** |
97 | - * Store an image into memory and disk cache at the given key. | 99 | + * Asynchronously store an image into memory and disk cache at the given key. |
98 | * | 100 | * |
99 | * @param image The image to store | 101 | * @param image The image to store |
100 | * @param key The unique image cache key, usually it's image absolute URL | 102 | * @param key The unique image cache key, usually it's image absolute URL |
103 | + * @param completion A block executed after the operation is finished | ||
101 | */ | 104 | */ |
102 | -- (void)storeImage:(nullable UIImage *)image forKey:(nullable NSString *)key; | 105 | +- (void)storeImage:(nullable UIImage *)image |
106 | + forKey:(nullable NSString *)key | ||
107 | + completion:(nullable SDWebImageNoParamsBlock)completionBlock; | ||
103 | 108 | ||
104 | /** | 109 | /** |
105 | - * Store an image into memory and optionally disk cache at the given key. | 110 | + * Asynchronously store an image into memory and disk cache at the given key. |
106 | * | 111 | * |
107 | * @param image The image to store | 112 | * @param image The image to store |
108 | * @param key The unique image cache key, usually it's image absolute URL | 113 | * @param key The unique image cache key, usually it's image absolute URL |
109 | * @param toDisk Store the image to disk cache if YES | 114 | * @param toDisk Store the image to disk cache if YES |
115 | + * @param completion A block executed after the operation is finished | ||
110 | */ | 116 | */ |
111 | -- (void)storeImage:(nullable UIImage *)image forKey:(nullable NSString *)key toDisk:(BOOL)toDisk; | 117 | +- (void)storeImage:(nullable UIImage *)image |
118 | + forKey:(nullable NSString *)key | ||
119 | + toDisk:(BOOL)toDisk | ||
120 | + completion:(nullable SDWebImageNoParamsBlock)completionBlock; | ||
112 | 121 | ||
113 | /** | 122 | /** |
114 | - * Store an image into memory and optionally disk cache at the given key. | 123 | + * Asynchronously store an image into memory and disk cache at the given key. |
115 | * | 124 | * |
116 | * @param image The image to store | 125 | * @param image The image to store |
117 | - * @param recalculate BOOL indicates if imageData can be used or a new data should be constructed from the UIImage | ||
118 | * @param imageData The image data as returned by the server, this representation will be used for disk storage | 126 | * @param imageData The image data as returned by the server, this representation will be used for disk storage |
119 | * instead of converting the given image object into a storable/compressed image format in order | 127 | * instead of converting the given image object into a storable/compressed image format in order |
120 | * to save quality and CPU | 128 | * to save quality and CPU |
121 | * @param key The unique image cache key, usually it's image absolute URL | 129 | * @param key The unique image cache key, usually it's image absolute URL |
122 | * @param toDisk Store the image to disk cache if YES | 130 | * @param toDisk Store the image to disk cache if YES |
131 | + * @param completion A block executed after the operation is finished | ||
123 | */ | 132 | */ |
124 | -- (void)storeImage:(nullable UIImage *)image recalculateFromImage:(BOOL)recalculate imageData:(nullable NSData *)imageData forKey:(nullable NSString *)key toDisk:(BOOL)toDisk; | 133 | +- (void)storeImage:(nullable UIImage *)image |
134 | + imageData:(nullable NSData *)imageData | ||
135 | + forKey:(nullable NSString *)key | ||
136 | + toDisk:(BOOL)toDisk | ||
137 | + completion:(nullable SDWebImageNoParamsBlock)completionBlock; | ||
125 | 138 | ||
126 | /** | 139 | /** |
127 | * Synchronously store image NSData into disk cache at the given key. | 140 | * Synchronously store image NSData into disk cache at the given key. |
@@ -176,8 +176,30 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { | @@ -176,8 +176,30 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { | ||
176 | return [paths[0] stringByAppendingPathComponent:fullNamespace]; | 176 | return [paths[0] stringByAppendingPathComponent:fullNamespace]; |
177 | } | 177 | } |
178 | 178 | ||
179 | -- (void)storeImage:(nullable UIImage *)image recalculateFromImage:(BOOL)recalculate imageData:(nullable NSData *)imageData forKey:(nullable NSString *)key toDisk:(BOOL)toDisk { | 179 | +#pragma mark - Store Ops |
180 | + | ||
181 | +- (void)storeImage:(nullable UIImage *)image | ||
182 | + forKey:(nullable NSString *)key | ||
183 | + completion:(nullable SDWebImageNoParamsBlock)completionBlock { | ||
184 | + [self storeImage:image imageData:nil forKey:key toDisk:YES completion:completionBlock]; | ||
185 | +} | ||
186 | + | ||
187 | +- (void)storeImage:(nullable UIImage *)image | ||
188 | + forKey:(nullable NSString *)key | ||
189 | + toDisk:(BOOL)toDisk | ||
190 | + completion:(nullable SDWebImageNoParamsBlock)completionBlock { | ||
191 | + [self storeImage:image imageData:nil forKey:key toDisk:toDisk completion:completionBlock]; | ||
192 | +} | ||
193 | + | ||
194 | +- (void)storeImage:(nullable UIImage *)image | ||
195 | + imageData:(nullable NSData *)imageData | ||
196 | + forKey:(nullable NSString *)key | ||
197 | + toDisk:(BOOL)toDisk | ||
198 | + completion:(nullable SDWebImageNoParamsBlock)completionBlock { | ||
180 | if (!image || !key) { | 199 | if (!image || !key) { |
200 | + if (completionBlock) { | ||
201 | + completionBlock(); | ||
202 | + } | ||
181 | return; | 203 | return; |
182 | } | 204 | } |
183 | // if memory cache is enabled | 205 | // if memory cache is enabled |
@@ -196,16 +218,17 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { | @@ -196,16 +218,17 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { | ||
196 | } | 218 | } |
197 | 219 | ||
198 | [self storeImageDataToDisk:data forKey:key]; | 220 | [self storeImageDataToDisk:data forKey:key]; |
221 | + if (completionBlock) { | ||
222 | + dispatch_async(dispatch_get_main_queue(), ^{ | ||
223 | + completionBlock(); | ||
199 | }); | 224 | }); |
200 | } | 225 | } |
201 | -} | ||
202 | - | ||
203 | -- (void)storeImage:(nullable UIImage *)image forKey:(nullable NSString *)key { | ||
204 | - [self storeImage:image recalculateFromImage:YES imageData:nil forKey:key toDisk:YES]; | ||
205 | -} | ||
206 | - | ||
207 | -- (void)storeImage:(nullable UIImage *)image forKey:(nullable NSString *)key toDisk:(BOOL)toDisk { | ||
208 | - [self storeImage:image recalculateFromImage:YES imageData:nil forKey:key toDisk:toDisk]; | 226 | + }); |
227 | + } else { | ||
228 | + if (completionBlock) { | ||
229 | + completionBlock(); | ||
230 | + } | ||
231 | + } | ||
209 | } | 232 | } |
210 | 233 | ||
211 | - (void)storeImageDataToDisk:(nullable NSData *)imageData forKey:(nullable NSString *)key { | 234 | - (void)storeImageDataToDisk:(nullable NSData *)imageData forKey:(nullable NSString *)key { |
@@ -218,7 +218,8 @@ | @@ -218,7 +218,8 @@ | ||
218 | 218 | ||
219 | if (transformedImage && finished) { | 219 | if (transformedImage && finished) { |
220 | BOOL imageWasTransformed = ![transformedImage isEqual:downloadedImage]; | 220 | BOOL imageWasTransformed = ![transformedImage isEqual:downloadedImage]; |
221 | - [self.imageCache storeImage:transformedImage recalculateFromImage:imageWasTransformed imageData:(imageWasTransformed ? nil : downloadedData) forKey:key toDisk:cacheOnDisk]; | 221 | + // pass nil if the image was transformed, so we can recalculate the data from the image |
222 | + [self.imageCache storeImage:transformedImage imageData:(imageWasTransformed ? nil : downloadedData) forKey:key toDisk:cacheOnDisk completion:nil]; | ||
222 | } | 223 | } |
223 | 224 | ||
224 | dispatch_main_sync_safe(^{ | 225 | dispatch_main_sync_safe(^{ |
@@ -229,7 +230,7 @@ | @@ -229,7 +230,7 @@ | ||
229 | }); | 230 | }); |
230 | } else { | 231 | } else { |
231 | if (downloadedImage && finished) { | 232 | if (downloadedImage && finished) { |
232 | - [self.imageCache storeImage:downloadedImage recalculateFromImage:NO imageData:downloadedData forKey:key toDisk:cacheOnDisk]; | 233 | + [self.imageCache storeImage:downloadedImage imageData:downloadedData forKey:key toDisk:cacheOnDisk completion:nil]; |
233 | } | 234 | } |
234 | 235 | ||
235 | dispatch_main_sync_safe(^{ | 236 | dispatch_main_sync_safe(^{ |
@@ -288,7 +289,7 @@ | @@ -288,7 +289,7 @@ | ||
288 | - (void)saveImageToCache:(nullable UIImage *)image forURL:(nullable NSURL *)url { | 289 | - (void)saveImageToCache:(nullable UIImage *)image forURL:(nullable NSURL *)url { |
289 | if (image && url) { | 290 | if (image && url) { |
290 | NSString *key = [self cacheKeyForURL:url]; | 291 | NSString *key = [self cacheKeyForURL:url]; |
291 | - [self.imageCache storeImage:image forKey:key toDisk:YES]; | 292 | + [self.imageCache storeImage:image forKey:key toDisk:YES completion:nil]; |
292 | } | 293 | } |
293 | } | 294 | } |
294 | 295 |
-
Please register or login to post a comment