SDWebImage是托管在Github上的:http://github.com/rs/SDWebImage
SDWebImage——简化网络图片处理
用SDWebImage调用网站上的图片,跟本地调用内置在应用包里的图片一样简单。操作也很简单,举例说明
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 behandled for you, from async downloads to caching management.
#import "UIImageView+WebCache.h"
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier] autorelease];
}
// 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"]];
cell.textLabel.text = @"My Text";
return cell;
}
The SDWebImageManager is the class behind the UIImageView+WebCache category. It ties theasynchronous downloader with the image cache store. You can use this classe directly to benefitsfrom web image downloading with caching in another context than a UIView (ie: with Cocos).
Here is a simple example of how to use SDWebImageManager:
SDWebImageManager *manager = [SDWebImageManager sharedManager];
UIImage *cachedImage = [manager imageWithURL:url];
if (cachedImage)
{
// Use the cached image immediatly
}
else
{
// Start an async download
[manager downloadWithURL:url delegate:self];
}
Your class will have to implement the SDWebImageManagerDelegate protocol, and to implement thewebImageManager:didFinishWithImage: method from this protocol:
- (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image
{
// Do something with the downloaded image
}
It is possible to use the async image downloader independently. You just have to create an instanceof SDWebImageDownloader using its convenience constructor downloaderWithURL:delegate:.
downloader = [SDWebImageDownloader downloaderWithURL:url delegate:self];
The download will start immediately and the imageDownloader:didFinishWithImage: method from theSDWebImageDownloaderDelegate protocol will be called as soon as the download of image is completed.
It is also possible to use the NSOperation based image cache store independently. SDImageCachemaintains a memory cache and an optional disk cache. Disk cache write operations are performedasynchronous so it doesn't add unnecessary latency to the UI.
The SDImageCache class provides a singleton instance for convenience but you can create your owninstance if you want to create separated cache namespaces.
To lookup the cache, you use the imageForKey: method. If the method returns nil, it means the cachedoesn't currently own the image. You are thus responsible of generating and caching it. The cachekey is an application unique identifier for the image to cache. It is generally the absolute URL ofthe image.
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 anegative second argument.
To store an image into the cache, you use the storeImage:forKey: method:
[[SDImageCache sharedImageCache] storeImage:myImage forKey:myCacheKey];
By default, the image will be stored in memory cache as well as on disk cache (asynchronously). Ifyou want only the memory cache, use the alternative method storeImage:forKey:toDisk: with a negativethird argument.