Changing AFImageCache APIs to use URLs rather than NSURLRequests

Completing documentation for AFImageCache
This commit is contained in:
Mattt Thompson 2011-09-21 23:46:00 -05:00
parent d67db7172a
commit 2a8cce88a5
4 changed files with 37 additions and 12 deletions

View file

@ -23,15 +23,40 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import "AFImageRequestOperation.h" #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 @interface AFImageCache : NSCache
/**
Returns the shared image cache object for the system.
@return The systemwide image cache.
*/
+ (AFImageCache *)sharedImageCache; + (AFImageCache *)sharedImageCache;
- (UIImage *)cachedImageForRequest:(NSURLRequest *)urlRequest /**
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; 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 - (void)cacheImage:(UIImage *)image
forRequest:(NSURLRequest *)urlRequest forURL:(NSURL *)url
cacheName:(NSString *)cacheName; cacheName:(NSString *)cacheName;
@end @end

View file

@ -22,8 +22,8 @@
#import "AFImageCache.h" #import "AFImageCache.h"
static inline NSString * AFImageCacheKey(NSURLRequest *urlRequest, NSString *cacheName) { static inline NSString * AFImageCacheKeyFromURLAndCacheName(NSURL *url, NSString *cacheName) {
return [[[urlRequest URL] absoluteString] stringByAppendingFormat:@"#%@", cacheName]; return [[url absoluteString] stringByAppendingFormat:@"#%@", cacheName];
} }
@implementation AFImageCache @implementation AFImageCache
@ -39,21 +39,21 @@ static inline NSString * AFImageCacheKey(NSURLRequest *urlRequest, NSString *cac
return _sharedImageCache; return _sharedImageCache;
} }
- (UIImage *)cachedImageForRequest:(NSURLRequest *)urlRequest - (UIImage *)cachedImageForURL:(NSURL *)url
cacheName:(NSString *)cacheName cacheName:(NSString *)cacheName
{ {
return [self objectForKey:AFImageCacheKey(urlRequest, cacheName)]; return [self objectForKey:AFImageCacheKeyFromURLAndCacheName(url, cacheName)];
} }
- (void)cacheImage:(UIImage *)image - (void)cacheImage:(UIImage *)image
forRequest:(NSURLRequest *)urlRequest forURL:(NSURL *)url
cacheName:(NSString *)cacheName cacheName:(NSString *)cacheName
{ {
if (!image) { if (!image) {
return; return;
} }
[self setObject:image forKey:AFImageCacheKey(urlRequest, cacheName)]; [self setObject:image forKey:AFImageCacheKeyFromURLAndCacheName(url, cacheName)];
} }
@end @end

View file

@ -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];
} }
}); });
}]; }];

View file

@ -120,7 +120,7 @@ static NSString * const kUIImageViewImageRequestObjectKey = @"_af_imageRequestOp
cacheName = [cacheName stringByAppendingFormat:@"(%@)", NSStringFromCGSize(placeholderImage.size)]; 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) { if (cachedImage) {
self.image = cachedImage; self.image = cachedImage;