Cache image data, as NSPurgeableData, rather than UIImage/NSImage

This commit is contained in:
Mattt Thompson 2011-10-25 08:49:06 -05:00
parent a37cda1032
commit d5a972e549
3 changed files with 13 additions and 37 deletions

View file

@ -57,21 +57,14 @@
#endif
/**
Stores an image into cache, associated with a given URL and cache name.
Stores image data into cache, associated with a given URL and cache name.
@param image The image to be stored in cache.
@param imageData The image data 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.
*/
#if __IPHONE_OS_VERSION_MIN_REQUIRED
- (void)cacheImage:(UIImage *)image
forURL:(NSURL *)url
cacheName:(NSString *)cacheName;
#elif __MAC_OS_X_VERSION_MIN_REQUIRED
- (void)cacheImage:(NSImage *)image
forURL:(NSURL *)url
cacheName:(NSString *)cacheName;
#endif
- (void)cacheImageData:(NSData *)imageData
forURL:(NSURL *)url
cacheName:(NSString *)cacheName;
@end

View file

@ -43,38 +43,21 @@ static inline NSString * AFImageCacheKeyFromURLAndCacheName(NSURL *url, NSString
- (UIImage *)cachedImageForURL:(NSURL *)url
cacheName:(NSString *)cacheName
{
return [self objectForKey:AFImageCacheKeyFromURLAndCacheName(url, cacheName)];
return [UIImage imageWithData:[self objectForKey:AFImageCacheKeyFromURLAndCacheName(url, cacheName)]];
}
#elif __MAC_OS_X_VERSION_MIN_REQUIRED
- (NSImage *)cachedImageForURL:(NSURL *)url
cacheName:(NSString *)cacheName
{
return [self objectForKey:AFImageCacheKeyFromURLAndCacheName(url, cacheName)];
return [[[NSImage alloc] initWithData:[self objectForKey:AFImageCacheKeyFromURLAndCacheName(url, cacheName)]] autorelease];
}
#endif
#if __IPHONE_OS_VERSION_MIN_REQUIRED
- (void)cacheImage:(UIImage *)image
forURL:(NSURL *)url
cacheName:(NSString *)cacheName
- (void)cacheImageData:(NSData *)imageData
forURL:(NSURL *)url
cacheName:(NSString *)cacheName
{
if (!image) {
return;
}
[self setObject:image forKey:AFImageCacheKeyFromURLAndCacheName(url, cacheName)];
[self setObject:[NSPurgeableData dataWithData:imageData] forKey:AFImageCacheKeyFromURLAndCacheName(url, cacheName)];
}
#elif __MAC_OS_X_VERSION_MIN_REQUIRED
- (void)cacheImage:(NSImage *)image
forURL:(NSURL *)url
cacheName:(NSString *)cacheName
{
if (!image) {
return;
}
[self setObject:image forKey:AFImageCacheKeyFromURLAndCacheName(url, cacheName)];
}
#endif
@end

View file

@ -104,7 +104,7 @@ static dispatch_queue_t image_request_operation_processing_queue() {
}
if ([operation.request cachePolicy] != NSURLCacheStorageNotAllowed) {
[[AFImageCache sharedImageCache] cacheImage:image forURL:[operation.request URL] cacheName:cacheNameOrNil];
[[AFImageCache sharedImageCache] cacheImageData:operation.responseData forURL:[operation.request URL] cacheName:cacheNameOrNil];
}
}
});
@ -147,7 +147,7 @@ static dispatch_queue_t image_request_operation_processing_queue() {
}
if ([operation.request cachePolicy] != NSURLCacheStorageNotAllowed) {
[[AFImageCache sharedImageCache] cacheImage:image forURL:[operation.request URL] cacheName:cacheNameOrNil];
[[AFImageCache sharedImageCache] cacheImageData:operation.responseData forURL:[operation.request URL] cacheName:cacheNameOrNil];
}
}
});