diff --git a/AFNetworking/AFImageCache.h b/AFNetworking/AFImageCache.h index f85a48f..08d9e3f 100644 --- a/AFNetworking/AFImageCache.h +++ b/AFNetworking/AFImageCache.h @@ -23,15 +23,40 @@ #import #import "AFImageRequestOperation.h" +/** + `AFImageCache` is a subclass of `NSCache` that stores and retrieves images from cache. + + @discussion `AFImageCache` is used to cache images for successful `AFImageRequestOperations` with the proper cache policy. + */ @interface AFImageCache : NSCache +/** + Returns the shared image cache object for the system. + + @return The systemwide image cache. + */ + (AFImageCache *)sharedImageCache; -- (UIImage *)cachedImageForRequest:(NSURLRequest *)urlRequest - cacheName:(NSString *)cacheName; +/** + Returns the image associated with a given URL and cache name. + + @param url The URL associated with the image in the cache. + @param cacheName The cache name associated with the image in the cache. This allows for multiple versions of an image to be associated for a single URL, such as image thumbnails, for instance. + + @return The image associated with the URL and cache name, or `nil` if not image exists. + */ +- (UIImage *)cachedImageForURL:(NSURL *)url + cacheName:(NSString *)cacheName; +/** + Stores an image into cache, associated with a given URL and cache name. + + @param image The image to be stored in cache. + @param url The URL to be associated with the image. + @param cacheName The cache name to be associated with the image in the cache. This allows for multiple versions of an image to be associated for a single URL, such as image thumbnails, for instance. + */ - (void)cacheImage:(UIImage *)image - forRequest:(NSURLRequest *)urlRequest + forURL:(NSURL *)url cacheName:(NSString *)cacheName; @end diff --git a/AFNetworking/AFImageCache.m b/AFNetworking/AFImageCache.m index 968b574..de74346 100644 --- a/AFNetworking/AFImageCache.m +++ b/AFNetworking/AFImageCache.m @@ -22,8 +22,8 @@ #import "AFImageCache.h" -static inline NSString * AFImageCacheKey(NSURLRequest *urlRequest, NSString *cacheName) { - return [[[urlRequest URL] absoluteString] stringByAppendingFormat:@"#%@", cacheName]; +static inline NSString * AFImageCacheKeyFromURLAndCacheName(NSURL *url, NSString *cacheName) { + return [[url absoluteString] stringByAppendingFormat:@"#%@", cacheName]; } @implementation AFImageCache @@ -39,21 +39,21 @@ static inline NSString * AFImageCacheKey(NSURLRequest *urlRequest, NSString *cac return _sharedImageCache; } -- (UIImage *)cachedImageForRequest:(NSURLRequest *)urlRequest - cacheName:(NSString *)cacheName +- (UIImage *)cachedImageForURL:(NSURL *)url + cacheName:(NSString *)cacheName { - return [self objectForKey:AFImageCacheKey(urlRequest, cacheName)]; + return [self objectForKey:AFImageCacheKeyFromURLAndCacheName(url, cacheName)]; } - (void)cacheImage:(UIImage *)image - forRequest:(NSURLRequest *)urlRequest + forURL:(NSURL *)url cacheName:(NSString *)cacheName { if (!image) { return; } - [self setObject:image forKey:AFImageCacheKey(urlRequest, cacheName)]; + [self setObject:image forKey:AFImageCacheKeyFromURLAndCacheName(url, cacheName)]; } @end diff --git a/AFNetworking/AFImageRequestOperation.m b/AFNetworking/AFImageRequestOperation.m index c2d5cf5..98867e8 100644 --- a/AFNetworking/AFImageRequestOperation.m +++ b/AFNetworking/AFImageRequestOperation.m @@ -77,7 +77,7 @@ static dispatch_queue_t image_request_operation_processing_queue() { } }); - [[AFImageCache sharedImageCache] cacheImage:image forRequest:request cacheName:cacheNameOrNil]; + [[AFImageCache sharedImageCache] cacheImage:image forURL:[request URL] cacheName:cacheNameOrNil]; } }); }]; diff --git a/AFNetworking/UIImageView+AFNetworking.m b/AFNetworking/UIImageView+AFNetworking.m index 7140fd5..dcd3a50 100644 --- a/AFNetworking/UIImageView+AFNetworking.m +++ b/AFNetworking/UIImageView+AFNetworking.m @@ -120,7 +120,7 @@ static NSString * const kUIImageViewImageRequestObjectKey = @"_af_imageRequestOp cacheName = [cacheName stringByAppendingFormat:@"(%@)", NSStringFromCGSize(placeholderImage.size)]; } - UIImage *cachedImage = [[AFImageCache sharedImageCache] cachedImageForRequest:urlRequest cacheName:cacheName]; + UIImage *cachedImage = [[AFImageCache sharedImageCache] cachedImageForURL:[urlRequest URL] cacheName:cacheName]; if (cachedImage) { self.image = cachedImage;