Who Use It

How To Use

Using UIImageView+WebCache category with UITableView

Just #import the UIImageView+WebCache.h header, and call the setImageWithURL:placeholderImage: method from the tableView:cellForRowAtIndexPath: UITableViewDataSource method. Everything will be handled for you, from async downloads to caching management.

#import <SDWebImage/UIImageView+WebCache.h>...-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{staticNSString*MyIdentifier=@"MyIdentifier";UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:MyIdentifier];if(cell==nil){cell=[[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:MyIdentifier]autorelease];}// Here we use the new provided setImageWithURL: method to load the web image[cell.imageViewsetImageWithURL:[NSURLURLWithString:@"http://www.domain.com/path/to/image.jpg"]placeholderImage:[UIImageimageNamed:@"placeholder.png"]];cell.textLabel.text=@"My Text";returncell;}

Using blocks

With blocks, you can be notified about the image download progress and whenever the image retrival has completed with success or not:

// Here we use the new provided setImageWithURL: method to load the web image[cell.imageViewsetImageWithURL:[NSURLURLWithString:@"http://www.domain.com/path/to/image.jpg"]placeholderImage:[UIImageimageNamed:@"placeholder.png"]completed:^(UIImage*image,NSError*error,SDImageCacheTypecacheType){...completioncodehere...}];

Using SDWebImageManager

The SDWebImageManager is the class behind the UIImageView+WebCache category. It ties the asynchronous downloader with the image cache store. You can use this class directly to benefit from web image downloading with caching in another context than a UIView (ie: with Cocoa).

Using Asynchronous Image Caching Independently

It is also possible to use the aync based image cache store independently. 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.

The SDImageCache class provides a singleton instance for convenience but you can create your own instance if you want to create separated cache namespace.

To lookup the cache, you use the imageForKey: method. If the method returns nil, it means the cache doesn't currently own the image. You are thus responsible for generating and caching it. The cache key is an application unique identifier for the image to cache. It is generally the absolute URL of the image.

SDImageCache*imageCache=[SDImageCache.allocinitWithNamespace:@"myNamespace"];[imageCachequeryDiskCacheForKey:myCacheKeydone:^(UIImage*image){// image is not nil if image was found}];

By default SDImageCache will lookup the disk cache if an image can't be found in the memory cache. You can prevent this from happening by calling the alternative method imageFromKey:fromDisk: with a negative second argument.

To store an image into the cache, you use the storeImage:forKey: method:

[[SDImageCachesharedImageCache]storeImage:myImageforKey:myCacheKey];

By default, the image will be stored in memory cache as well as on disk cache (asynchronously). If you want only the memory cache, use the alternative method storeImage:forKey:toDisk: with a negative third argument.

Using cache key filter

Sometime, you may not want to use the image URL as cache key because part of the URL is dynamic (i.e.: for access control purpose). SDWebImageManager provides a way to set a cache key filter that takes the NSURL as input, and output a cache key NSString.

The following example sets a filter in the application delegate that will remove any query-string from the URL before to use it as a cache key:

Common Problems

Using dynamic image size with UITableViewCell

UITableView determins the size of the image by the first image set for a cell. If your remote images don't have the same size as your placeholder image, you may experience strange anamorphic scaling issue. The following article gives a way to workaround this issue: