Inherits from NSObject
Declared in SDImageCache.h
SDImageCache.m

Overview

SDImageCache maintains a memory cache and an optional disk cache. Disk cache write operations are performed asynchronous so it doesn’t add unnecessary latency to the UI.

Properties

maxCacheAge

The maximum length of time to keep an image in the cache, in seconds

@property (assign, nonatomic) NSInteger maxCacheAge

Declared In

SDImageCache.h

maxCacheSize

The maximum size of the cache, in bytes.

@property (assign, nonatomic) unsigned long long maxCacheSize

Declared In

SDImageCache.h

maxMemoryCost

The maximum “total cost” of the in-memory image cache. The cost function is the number of pixels held in memory.

@property (assign, nonatomic) NSUInteger maxMemoryCost

Declared In

SDImageCache.h

Class Methods

sharedImageCache

Returns global shared cache instance

+ (SDImageCache *)sharedImageCache

Return Value

SDImageCache global instance

Declared In

SDImageCache.h

Instance Methods

addReadOnlyCachePath:

Add a read-only cache path to search for images pre-cached by SDImageCache Useful if you want to bundle pre-loaded images with your app

- (void)addReadOnlyCachePath:(NSString *)path

Parameters

path

The path to use for this read-only cache path

Declared In

SDImageCache.h

calculateSizeWithCompletionBlock:

Asynchronously calculate the disk cache’s size.

- (void)calculateSizeWithCompletionBlock:(void ( ^ ) ( NSUInteger fileCount , unsigned long long totalSize ))completionBlock

Declared In

SDImageCache.h

cleanDisk

Remove all expired cached image from disk

- (void)cleanDisk

Declared In

SDImageCache.h

clearDisk

Clear all disk cached images

- (void)clearDisk

Declared In

SDImageCache.h

clearMemory

Clear all memory cached images

- (void)clearMemory

Declared In

SDImageCache.h

diskImageExistsWithKey:

Check if image exists in cache already

- (BOOL)diskImageExistsWithKey:(NSString *)key

Declared In

SDImageCache.h

getDiskCount

Get the number of images in the disk cache

- (int)getDiskCount

Declared In

SDImageCache.h

getSize

Get the size used by the disk cache

- (unsigned long long)getSize

Declared In

SDImageCache.h

imageFromDiskCacheForKey:

Query the disk cache synchronously after checking the memory cache.

- (UIImage *)imageFromDiskCacheForKey:(NSString *)key

Parameters

key

The unique key used to store the wanted image

Declared In

SDImageCache.h

imageFromMemoryCacheForKey:

Query the memory cache synchronously.

- (UIImage *)imageFromMemoryCacheForKey:(NSString *)key

Parameters

key

The unique key used to store the wanted image

Declared In

SDImageCache.h

initWithNamespace:

Init a new cache store with a specific namespace

- (id)initWithNamespace:(NSString *)ns

Parameters

ns

The namespace to use for this cache store

Declared In

SDImageCache.h

queryDiskCacheForKey:done:

Query the disk cache asynchronously.

- (NSOperation *)queryDiskCacheForKey:(NSString *)key done:(void ( ^ ) ( UIImage *image , SDImageCacheType cacheType ))doneBlock

Parameters

key

The unique key used to store the wanted image

Declared In

SDImageCache.h

removeImageForKey:

Remove the image from memory and disk cache synchronously

- (void)removeImageForKey:(NSString *)key

Parameters

key

The unique image cache key

Declared In

SDImageCache.h

removeImageForKey:fromDisk:

Remove the image from memory and optionaly disk cache synchronously

- (void)removeImageForKey:(NSString *)key fromDisk:(BOOL)fromDisk

Parameters

key

The unique image cache key

fromDisk

Also remove cache entry from disk if YES

Declared In

SDImageCache.h

storeImage:forKey:

Store an image into memory and disk cache at the given key.

- (void)storeImage:(UIImage *)image forKey:(NSString *)key

Parameters

image

The image to store

key

The unique image cache key, usually it’s image absolute URL

Declared In

SDImageCache.h

storeImage:forKey:toDisk:

Store an image into memory and optionally disk cache at the given key.

- (void)storeImage:(UIImage *)image forKey:(NSString *)key toDisk:(BOOL)toDisk

Parameters

image

The image to store

key

The unique image cache key, usually it’s image absolute URL

toDisk

Store the image to disk cache if YES

Declared In

SDImageCache.h

storeImage:recalculateFromImage:imageData:forKey:toDisk:

Store an image into memory and optionally disk cache at the given key.

- (void)storeImage:(UIImage *)image recalculateFromImage:(BOOL)recalculate imageData:(NSData *)imageData forKey:(NSString *)key toDisk:(BOOL)toDisk

Parameters

image

The image to store

recalculate

BOOL indicates if imageData can be used or a new data should be constructed from the UIImage

imageData

The image data as returned by the server, this representation will be used for disk storage instead of converting the given image object into a storable/compressed image format in order to save quality and CPU

key

The unique image cache key, usually it’s image absolute URL

toDisk

Store the image to disk cache if YES

Declared In

SDImageCache.h