2016-05-10 13:39:46 +08:00
SDWebImage
==========
2014-07-04 15:56:09 +08:00
[![Build Status ](http://img.shields.io/travis/rs/SDWebImage/master.svg?style=flat )](https://travis-ci.org/rs/SDWebImage)
[![Pod Version ](http://img.shields.io/cocoapods/v/SDWebImage.svg?style=flat )](http://cocoadocs.org/docsets/SDWebImage/)
[![Pod Platform ](http://img.shields.io/cocoapods/p/SDWebImage.svg?style=flat )](http://cocoadocs.org/docsets/SDWebImage/)
[![Pod License ](http://img.shields.io/cocoapods/l/SDWebImage.svg?style=flat )](https://www.apache.org/licenses/LICENSE-2.0.html)
2014-07-26 16:44:56 +08:00
[![Dependency Status ](https://www.versioneye.com/objective-c/sdwebimage/3.3/badge.svg?style=flat )](https://www.versioneye.com/objective-c/sdwebimage/3.3)
2014-07-26 16:45:46 +08:00
[![Reference Status ](https://www.versioneye.com/objective-c/sdwebimage/reference_badge.svg?style=flat )](https://www.versioneye.com/objective-c/sdwebimage/references)
2015-03-15 03:06:39 +08:00
[![Carthage compatible ](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat )](https://github.com/rs/SDWebImage)
2009-09-20 02:45:42 +08:00
2016-05-10 13:39:46 +08:00
This library provides an async image downloader with cache support. For convenience, we added categories for some `UIControl` elements like `UIImageView` .
2009-09-20 02:45:42 +08:00
2016-05-10 13:39:46 +08:00
## Features
2009-09-21 10:29:00 +08:00
2016-05-10 13:39:46 +08:00
- [x] Categories for `UIImageView` , `UIButton` , `MKAnnotationView` adding web image and cache management
- [x] An asynchronous image downloader
- [x] An asynchronous memory + disk image caching with automatic cache expiration handling
- [x] A background image decompression
- [x] A guarantee that the same URL won't be downloaded several times
- [x] A guarantee that bogus URLs won't be retried again and again
- [x] A guarantee that main thread will never be blocked
- [x] Performances!
- [x] Use GCD and ARC
2012-11-04 16:50:23 +08:00
2016-05-10 13:39:46 +08:00
## Supported Image Formats
2009-09-20 03:14:40 +08:00
2016-05-10 13:39:46 +08:00
- Image formats supported by UIImage (JPEG, PNG, ...), including GIF
- WebP format (use the `WebP` subspec)
2012-11-04 23:17:01 +08:00
2016-05-10 13:39:46 +08:00
## Requirements
2012-11-04 22:17:32 +08:00
2016-05-10 13:39:46 +08:00
- iOS 5.1.1+ / tvOS 9.0+
- Xcode 7.1+
2012-11-04 22:17:32 +08:00
2016-05-10 13:39:46 +08:00
#### Backwards compatibility
2009-09-20 03:14:40 +08:00
2016-05-10 13:39:46 +08:00
- For iOS < 5.0 , please use the last [2.0 version ](https://github.com/rs/SDWebImage/tree/2.0-compat ).
2012-05-10 20:16:10 +08:00
2016-05-10 13:39:46 +08:00
## Getting Started
2009-09-21 10:29:00 +08:00
2016-05-10 13:39:46 +08:00
- Read this Readme doc
- Read the [How to use section ](https://github.com/rs/SDWebImage#how-to-use )
- Read the [documentation @ CocoaDocs ](http://cocoadocs.org/docsets/SDWebImage/ )
- Read [How is SDWebImage better than X? ](https://github.com/rs/SDWebImage/wiki/How-is-SDWebImage-better-than-X%3F )
- Try the example by downloading the project from Github or even easier using CocoaPods try `pod try SDWebImage`
- Get to the [installation steps ](https://github.com/rs/SDWebImage#installation )
## Who Uses It
- Find out [who uses SDWebImage ](https://github.com/rs/SDWebImage/wiki/Who-Uses-SDWebImage ) and add your app to the list.
## Installation
## How To Use
#### Using `UIImageView+WebCache` category with `UITableView`
Just import the `UIImageView+WebCache.h` header, and call the `sd_setImageWithURL:placeholderImage:`
method from the `tableView:cellForRowAtIndexPath:` `UITableViewDataSource` method. Everything will be
2010-06-09 10:09:18 +08:00
handled for you, from async downloads to caching management.
2009-09-21 10:29:00 +08:00
2012-05-09 17:33:38 +08:00
```objective-c
2012-05-10 06:30:48 +08:00
#import <SDWebImage/UIImageView+WebCache.h>
2009-09-21 10:29:00 +08:00
2012-05-09 17:33:38 +08:00
...
2009-09-23 09:05:40 +08:00
2015-10-30 04:02:22 +08:00
- (UITableViewCell *)tableView:(UITableView * )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
2012-05-09 17:33:38 +08:00
static NSString *MyIdentifier = @"MyIdentifier";
2009-09-23 09:05:40 +08:00
2012-05-09 17:33:38 +08:00
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
2015-10-30 04:02:22 +08:00
if (cell == nil) {
2012-05-09 17:33:38 +08:00
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier] autorelease];
}
2009-09-23 09:05:40 +08:00
2015-03-09 10:14:43 +08:00
// Here we use the new provided sd_setImageWithURL: method to load the web image
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
2009-09-23 09:05:40 +08:00
2012-05-09 17:33:38 +08:00
cell.textLabel.text = @"My Text";
return cell;
}
```
2009-09-21 10:29:00 +08:00
2016-05-10 13:39:46 +08:00
```swift
import SDWebImage
...
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
static let myIdentifier = "MyIdentifier"
let cell = tableView.dequeueReusableCellWithIdentifier(myIdentifier, forIndexPath: indexPath) as UITableViewCell
cell.imageView.sd_setImageWithURL(imageUrl, placeholderImage:placeholderImage)
return cell
```
2012-03-11 03:15:06 +08:00
### Using blocks
2016-05-10 13:39:46 +08:00
With blocks, you can be notified about the image download progress and whenever the image retrieval has completed with success or not:
2012-11-04 16:50:23 +08:00
2012-05-09 17:33:38 +08:00
```objective-c
2015-03-09 10:14:43 +08:00
// Here we use the new provided sd_setImageWithURL: method to load the web image
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
2016-05-10 13:39:46 +08:00
placeholderImage:[UIImage imageNamed:@"placeholder.png"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
2015-10-30 04:02:22 +08:00
... completion code here ...
}];
2012-05-09 17:33:38 +08:00
```
2012-03-11 03:15:06 +08:00
Note: neither your success nor failure block will be call if your image request is canceled before completion.
2009-09-24 05:22:48 +08:00
### Using SDWebImageManager
2016-05-10 13:39:46 +08:00
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).
2009-09-24 05:22:48 +08:00
2016-05-10 13:39:46 +08:00
Here is a simple example of how to use `SDWebImageManager` :
2009-09-24 05:22:48 +08:00
2012-05-09 17:33:38 +08:00
```objective-c
SDWebImageManager *manager = [SDWebImageManager sharedManager];
2015-06-30 03:42:08 +08:00
[manager loadImageWithURL:imageURL
options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize) {
// progression tracking code
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
if (image) {
// do something with image
}
}];
2012-05-09 17:33:38 +08:00
```
2009-09-24 05:22:48 +08:00
### Using Asynchronous Image Downloader Independently
2009-09-21 10:29:00 +08:00
2012-11-04 16:50:23 +08:00
It's also possible to use the async image downloader independently:
2012-05-09 17:33:38 +08:00
```objective-c
2015-10-24 19:08:56 +08:00
SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader];
[downloader downloadImageWithURL:imageURL
options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize) {
// progression tracking code
}
completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
if (image & & finished) {
// do something with image
}
}];
2012-05-09 17:33:38 +08:00
```
2009-09-21 10:29:00 +08:00
2009-09-24 05:22:48 +08:00
### Using Asynchronous Image Caching Independently
2009-09-21 10:29:00 +08:00
2015-02-10 20:24:33 +08:00
It is also possible to use the async based image cache store independently. SDImageCache
2009-09-21 22:03:45 +08:00
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.
2009-09-21 10:29:00 +08:00
2009-09-22 01:34:32 +08:00
The SDImageCache class provides a singleton instance for convenience but you can create your own
2011-10-20 21:03:19 +08:00
instance if you want to create separated cache namespace.
2009-09-21 10:29:00 +08:00
2014-06-11 16:44:45 +08:00
To lookup the cache, you use the `queryDiskCacheForKey:done:` method. If the method returns nil, it means the cache
2011-10-20 21:03:19 +08:00
doesn't currently own the image. You are thus responsible for generating and caching it. The cache
2009-09-21 22:03:45 +08:00
key is an application unique identifier for the image to cache. It is generally the absolute URL of
the image.
2009-09-21 10:29:00 +08:00
2012-05-09 17:33:38 +08:00
```objective-c
2014-01-07 09:30:28 +08:00
SDImageCache *imageCache = [[SDImageCache alloc] initWithNamespace:@"myNamespace"];
2015-10-30 04:02:22 +08:00
[imageCache queryDiskCacheForKey:myCacheKey done:^(UIImage *image) {
2012-11-04 16:50:23 +08:00
// image is not nil if image was found
}];
2012-05-09 17:33:38 +08:00
```
2009-09-21 10:29:00 +08:00
2009-09-22 01:34:32 +08:00
By default SDImageCache will lookup the disk cache if an image can't be found in the memory cache.
2013-01-16 18:41:59 +08:00
You can prevent this from happening by calling the alternative method `imageFromMemoryCacheForKey:` .
2009-09-21 10:29:00 +08:00
To store an image into the cache, you use the storeImage:forKey: method:
2012-05-09 17:33:38 +08:00
```objective-c
[[SDImageCache sharedImageCache] storeImage:myImage forKey:myCacheKey];
```
2009-09-20 03:14:40 +08:00
2009-09-21 22:03:45 +08:00
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.
2009-09-20 03:14:40 +08:00
2012-05-09 17:04:09 +08:00
### 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:
2012-05-09 17:33:38 +08:00
```objective-c
2015-10-30 04:02:22 +08:00
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary * )launchOptions {
2014-09-11 02:51:25 +08:00
SDWebImageManager.sharedManager.cacheKeyFilter = ^(NSURL *url) {
url = [[NSURL alloc] initWithScheme:url.scheme host:url.host path:url.path];
2012-05-09 17:33:38 +08:00
return [url absoluteString];
2012-11-04 16:50:23 +08:00
};
2012-05-09 17:33:38 +08:00
// Your app init code...
return YES;
}
```
2012-05-09 17:04:09 +08:00
2012-03-22 17:33:07 +08:00
Common Problems
---------------
### Using dynamic image size with UITableViewCell
2014-12-20 02:14:08 +08:00
UITableView determines the size of the image by the first image set for a cell. If your remote images
2012-03-22 17:33:07 +08:00
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:
[http://www.wrichards.com/blog/2011/11/sdwebimage-fixed-width-cell-images/ ](http://www.wrichards.com/blog/2011/11/sdwebimage-fixed-width-cell-images/ )
2012-03-11 00:40:02 +08:00
2013-03-13 07:06:03 +08:00
### Handle image refresh
SDWebImage does very aggressive caching by default. It ignores all kind of caching control header returned by the HTTP server and cache the returned images with no time restriction. It implies your images URLs are static URLs pointing to images that never change. If the pointed image happen to change, some parts of the URL should change accordingly.
If you don't control the image server you're using, you may not be able to change the URL when its content is updated. This is the case for Facebook avatar URLs for instance. In such case, you may use the `SDWebImageRefreshCached` flag. This will slightly degrade the performance but will respect the HTTP caching control headers:
``` objective-c
2015-03-09 10:14:43 +08:00
[imageView sd_setImageWithURL:[NSURL URLWithString:@"https://graph.facebook.com/olivier.poitrey/picture"]
placeholderImage:[UIImage imageNamed:@"avatar-placeholder.png"]
options:SDWebImageRefreshCached];
2013-03-13 07:06:03 +08:00
```
2013-05-06 20:11:26 +08:00
### Add a progress indicator
See this category: https://github.com/JJSaccolo/UIActivityIndicator-for-SDWebImage
2013-03-13 07:06:03 +08:00
2012-01-28 06:41:43 +08:00
Installation
------------
2014-05-28 14:46:40 +08:00
There are three ways to use SDWebImage in your project:
2016-02-11 05:38:58 +08:00
- using CocoaPods
2014-05-28 14:46:40 +08:00
- copying all the files into your project
- importing the project as a static library
### Installation with CocoaPods
[CocoaPods ](http://cocoapods.org/ ) is a dependency manager for Objective-C, which automates and simplifies the process of using 3rd-party libraries in your projects. See the [Get Started ](http://cocoapods.org/#get_started ) section for more details.
#### Podfile
```
platform :ios, '6.1'
2015-02-24 17:49:22 +08:00
pod 'SDWebImage', '~>3.7'
2014-05-28 14:46:40 +08:00
```
2012-01-28 06:41:43 +08:00
2015-09-22 11:10:14 +08:00
If you are using Swift, be sure to add `use_frameworks!` and set your target to iOS 8+:
```
platform :ios, '8.0'
use_frameworks!
```
2015-10-06 21:59:48 +08:00
#### Subspecs
There are 3 subspecs available now: `Core` , `MapKit` and `WebP` (this means you can install only some of the SDWebImage modules. By default, you get just `Core` , so if you need `WebP` , you need to specify it).
Podfile example:
```
2015-10-26 17:39:19 +08:00
pod 'SDWebImage/WebP'
2015-10-06 21:59:48 +08:00
```
2015-03-15 03:06:39 +08:00
### Installation with Carthage (iOS 8+)
2015-09-12 02:59:51 +08:00
[Carthage ](https://github.com/Carthage/Carthage ) is a lightweight dependency manager for Swift and Objective-C. It leverages CocoaTouch modules and is less invasive than CocoaPods.
2015-03-15 03:06:39 +08:00
To install with carthage, follow the instruction on [Carthage ](https://github.com/Carthage/Carthage )
#### Cartfile
```
github "rs/SDWebImage"
```
2014-11-06 20:20:35 +08:00
### Installation by cloning the repository
2016-05-10 13:39:46 +08:00
- see [Manual install ](https://raw.github.com/rs/SDWebImage/master/ManualInstallation.md )
2014-08-27 20:10:27 +08:00
2012-05-10 06:30:48 +08:00
### Import headers in your source files
2012-09-02 18:40:21 +08:00
In the source files where you need to use the library, import the header file:
2012-05-10 06:30:48 +08:00
```objective-c
#import <SDWebImage/UIImageView+WebCache.h>
```
2012-01-28 06:41:43 +08:00
2012-04-26 06:05:43 +08:00
### Build Project
2012-05-10 06:30:48 +08:00
At this point your workspace should build without error. If you are having problem, post to the Issue and the
community can help you solve it.
2012-04-26 06:05:43 +08:00
2009-09-20 03:14:40 +08:00
Future Enhancements
-------------------
2012-11-04 23:17:01 +08:00
- LRU memory cache cleanup instead of reset on memory warning
2012-11-05 00:47:56 +08:00
## Licenses
2012-11-05 19:20:18 +08:00
All source code is licensed under the [MIT License ](https://raw.github.com/rs/SDWebImage/master/LICENSE ).
2016-05-10 13:39:46 +08:00
## Architecture
< p align = "center" >
< img src = "SDWebImageClassDiagram.png" title = "SDWebImage class diagram" width = 800 >
< / p >