Inherits from NSObject
Declared in SDImageCache.h

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.

Tasks

Properties

maxCacheAge

@property (assign, nonatomic) NSInteger maxCacheAge
Discussion

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

Declared In

SDImageCache.h

maxCacheSize

@property (assign, nonatomic) NSUInteger maxCacheSize
Discussion

The maximum size of the cache, in bytes.

Declared In

SDImageCache.h

maxMemoryCost

@property (assign, nonatomic) NSUInteger maxMemoryCost
Discussion

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

Declared In

SDImageCache.h

maxMemoryCountLimit

@property (assign, nonatomic) NSUInteger maxMemoryCountLimit
Discussion

The maximum number of objects the cache should hold.

Declared In

SDImageCache.h

shouldDecompressImages

@property (assign, nonatomic) BOOL shouldDecompressImages
Discussion

Decompressing images that are downloaded and cached can improve peformance but can consume lot of memory. Defaults to YES. Set this to NO if you are experiencing a crash due to excessive memory consumption.

Declared In

SDImageCache.h

Class Methods

sharedImageCache

+ (SDImageCache *)sharedImageCache
Discussion

Returns global shared cache instance

Return Value

SDImageCache global instance

Declared In

SDImageCache.h

Instance Methods

addReadOnlyCachePath:

- (void)addReadOnlyCachePath:(NSString *)path
Discussion

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

Parameters

path

The path to use for this read-only cache path

Declared In

SDImageCache.h

cachePathForKey:inPath:

- (NSString *)cachePathForKey:(NSString *)key inPath:(NSString *)path
Discussion

Get the cache path for a certain key (needs the cache path root folder)

Parameters

key

the key (can be obtained from url using cacheKeyForURL)

path

the cach path root folder

Return Value

the cache path

Declared In

SDImageCache.h

calculateSizeWithCompletionBlock:

- (void)calculateSizeWithCompletionBlock:(SDWebImageCalculateSizeBlock)completionBlock
Discussion

Asynchronously calculate the disk cache’s size.

Declared In

SDImageCache.h

cleanDisk

- (void)cleanDisk
Discussion

Remove all expired cached image from disk

Declared In

SDImageCache.h

cleanDiskWithCompletionBlock:

- (void)cleanDiskWithCompletionBlock:(SDWebImageNoParamsBlock)completionBlock
Discussion

Remove all expired cached image from disk. Non-blocking method - returns immediately.

Parameters

completionBlock

An block that should be executed after cache expiration completes (optional)

Declared In

SDImageCache.h

clearDisk

- (void)clearDisk
Discussion

Clear all disk cached images

Declared In

SDImageCache.h

clearDiskOnCompletion:

- (void)clearDiskOnCompletion:(SDWebImageNoParamsBlock)completion
Discussion

Clear all disk cached images. Non-blocking method - returns immediately.

Parameters

completion

An block that should be executed after cache expiration completes (optional)

Declared In

SDImageCache.h

clearMemory

- (void)clearMemory
Discussion

Clear all memory cached images

Declared In

SDImageCache.h

defaultCachePathForKey:

- (NSString *)defaultCachePathForKey:(NSString *)key
Discussion

Get the default cache path for a certain key

Parameters

key

the key (can be obtained from url using cacheKeyForURL)

Return Value

the default cache path

Declared In

SDImageCache.h

diskImageExistsWithKey:

- (BOOL)diskImageExistsWithKey:(NSString *)key
Discussion

Check if image exists in disk cache already (does not load the image)

Parameters

key

the key describing the url

Return Value

YES if an image exists for the given key

Declared In

SDImageCache.h

diskImageExistsWithKey:completion:

- (void)diskImageExistsWithKey:(NSString *)key completion:(SDWebImageCheckCacheCompletionBlock)completionBlock
Discussion

Async check if image exists in disk cache already (does not load the image)

Note: the completion block will be always executed on the main queue

Parameters

key

the key describing the url

completionBlock

the block to be executed when the check is done.

Declared In

SDImageCache.h

getDiskCount

- (NSUInteger)getDiskCount
Discussion

Get the number of images in the disk cache

Declared In

SDImageCache.h

getSize

- (NSUInteger)getSize
Discussion

Get the size used by the disk cache

Declared In

SDImageCache.h

imageFromDiskCacheForKey:

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

Query the disk cache synchronously after checking the memory cache.

Parameters

key

The unique key used to store the wanted image

Declared In

SDImageCache.h

imageFromMemoryCacheForKey:

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

Query the memory cache synchronously.

Parameters

key

The unique key used to store the wanted image

Declared In

SDImageCache.h

initWithNamespace:

- (id)initWithNamespace:(NSString *)ns
Discussion

Init a new cache store with a specific namespace

Parameters

ns

The namespace to use for this cache store

Declared In

SDImageCache.h

initWithNamespace:diskCacheDirectory:

- (id)initWithNamespace:(NSString *)ns diskCacheDirectory:(NSString *)directory
Discussion

Init a new cache store with a specific namespace and directory

Parameters

ns

The namespace to use for this cache store

directory

Directory to cache disk images in

Declared In

SDImageCache.h

makeDiskCachePath:

- (NSString *)makeDiskCachePath:(NSString *)fullNamespace

queryDiskCacheForKey:done:

- (NSOperation *)queryDiskCacheForKey:(NSString *)key done:(SDWebImageQueryCompletedBlock)doneBlock
Discussion

Query the disk cache asynchronously.

Parameters

key

The unique key used to store the wanted image

Declared In

SDImageCache.h

removeImageForKey:

- (void)removeImageForKey:(NSString *)key
Discussion

Remove the image from memory and disk cache synchronously

Parameters

key

The unique image cache key

Declared In

SDImageCache.h

removeImageForKey:fromDisk:

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

Remove the image from memory and optionally disk cache asynchronously

Parameters

key

The unique image cache key

fromDisk

Also remove cache entry from disk if YES

Declared In

SDImageCache.h

removeImageForKey:fromDisk:withCompletion:

- (void)removeImageForKey:(NSString *)key fromDisk:(BOOL)fromDisk withCompletion:(SDWebImageNoParamsBlock)completion
Discussion

Remove the image from memory and optionally disk cache asynchronously

Parameters

key

The unique image cache key

fromDisk

Also remove cache entry from disk if YES

completion

An block that should be executed after the image has been removed (optional)

Declared In

SDImageCache.h

removeImageForKey:withCompletion:

- (void)removeImageForKey:(NSString *)key withCompletion:(SDWebImageNoParamsBlock)completion
Discussion

Remove the image from memory and disk cache asynchronously

Parameters

key

The unique image cache key

completion

An block that should be executed after the image has been removed (optional)

Declared In

SDImageCache.h

storeImage:forKey:

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

Store an image into memory and disk cache at the given 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:

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

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

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:

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

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

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