Update README.md with syntax color.

This commit is contained in:
Eli Wang 2012-05-09 17:33:38 +08:00 committed by Olivier Poitrey
parent dd5f15055d
commit 16d661488a
1 changed files with 67 additions and 53 deletions

View File

@ -64,6 +64,7 @@ Just #import the UIImageView+WebCache.h header, and call the setImageWithURL:pla
method from the tableView:cellForRowAtIndexPath: UITableViewDataSource method. Everything will be
handled for you, from async downloads to caching management.
```objective-c
#import "UIImageView+WebCache.h"
...
@ -87,18 +88,20 @@ handled for you, from async downloads to caching management.
cell.textLabel.text = @"My Text";
return cell;
}
```
### Using blocks
If your project's deployement target is set to iOS 4+, you may want to use the success/failure blocks to be
notified when image have been retrieved from cache.
```objective-c
// Here we use the new provided setImageWithURL: method to load the web image
[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]
success:^(UIImage *image) {... success code here ...}
failure:^(NSError *error) {... failure code here ...}];
];
```
Note: neither your success nor failure block will be call if your image request is canceled before completion.
@ -110,6 +113,7 @@ from web image downloading with caching in another context than a UIView (ie: wi
Here is a simple example of how to use SDWebImageManager:
```objective-c
SDWebImageManager *manager = [SDWebImageManager sharedManager];
UIImage *cachedImage = [manager imageWithURL:url];
@ -123,21 +127,25 @@ Here is a simple example of how to use SDWebImageManager:
// Start an async download
[manager downloadWithURL:url delegate:self];
}
```
Your class will have to implement the SDWebImageManagerDelegate protocol, and to implement the
webImageManager:didFinishWithImage: method from this protocol:
```objective-c
- (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image
{
// Do something with the downloaded image
}
```
### Using Asynchronous Image Downloader Independently
It is possible to use the async image downloader independently. You just have to create an instance
of SDWebImageDownloader using its convenience constructor downloaderWithURL:delegate:.
```objective-c
downloader = [SDWebImageDownloader downloaderWithURL:url delegate:self];
```
The download will start immediately and the imageDownloader:didFinishWithImage: method from the
SDWebImageDownloaderDelegate protocol will be called as soon as the download of image is completed.
@ -156,7 +164,9 @@ doesn't currently own the image. You are thus responsible for generating and cac
key is an application unique identifier for the image to cache. It is generally the absolute URL of
the image.
```objective-c
UIImage *myCachedImage = [[SDImageCache sharedImageCache] imageFromKey:myCacheKey];
```
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
@ -164,7 +174,9 @@ negative second argument.
To store an image into the cache, you use the storeImage:forKey: method:
```objective-c
[[SDImageCache sharedImageCache] storeImage:myImage forKey: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
@ -179,6 +191,7 @@ 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:
```objective-c
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[SDWebImageManager sharedManager] setCacheKeyFilter:^(NSURL *url)
@ -190,6 +203,7 @@ the URL before to use it as a cache key:
// Your app init code...
return YES;
}
```
Common Problems