Browse Source

Ask <AFImageRequestCache> implementor if an image should be cached. Implementors may prevent caching.

Henddher Pedroza 8 năm trước cách đây
mục cha
commit
51a80847ed

+ 11 - 0
UIKit+AFNetworking/AFAutoPurgingImageCache.h

@@ -72,6 +72,17 @@ NS_ASSUME_NONNULL_BEGIN
  */
 @protocol AFImageRequestCache <AFImageCache>
 
+/**
+ Asks if the image should be cached using an identifier created from the request and additional identifier.
+ 
+ @param image The image to be cached.
+ @param request The unique URL request identifing the image asset.
+ @param identifier The additional identifier to apply to the URL request to identify the image.
+ 
+ @return A BOOL indicating whether or not the image should be added to the cache. YES will cache, NO will prevent caching.
+ */
+- (BOOL)shouldCacheImage:(UIImage *)image forRequest:(NSURLRequest *)request withAdditionalIdentifier:(nullable NSString *)identifier;
+
 /**
  Adds the image to the cache using an identifier created from the request and additional identifier.
 

+ 4 - 0
UIKit+AFNetworking/AFAutoPurgingImageCache.m

@@ -196,6 +196,10 @@
     return key;
 }
 
+- (BOOL)shouldCacheImage:(UIImage *)image forRequest:(NSURLRequest *)request withAdditionalIdentifier:(nullable NSString *)identifier {
+    return YES;
+}
+
 @end
 
 #endif

+ 3 - 1
UIKit+AFNetworking/AFImageDownloader.m

@@ -266,7 +266,9 @@
                                            }
                                        }
                                    } else {
-                                       [strongSelf.imageCache addImage:responseObject forRequest:request withAdditionalIdentifier:nil];
+                                       if ([strongSelf.imageCache shouldCacheImage:responseObject forRequest:request withAdditionalIdentifier:nil]) {
+                                           [strongSelf.imageCache addImage:responseObject forRequest:request withAdditionalIdentifier:nil];
+                                       }
 
                                        for (AFImageDownloaderResponseHandler *handler in mergedTask.responseHandlers) {
                                            if (handler.successBlock) {