Authored by Olivier Poitrey

Replace the SDWebImageView by an UIImageView category make the integration even simpler

Web Image
=========
This library provides a drop-in remplacement for UIImageVIew with support for remote images coming
from the web.
This library provides a category for UIImageVIew with support for remote images coming from the web.
It provides:
... ... @@ -58,18 +57,39 @@ it, et voila!
How To Use It
-------------
### SDWebImageView as UIImageWeb Drop-In Replacement
### Using UIImageView+WebCache category with UITableView
Most common use is in conjunction with an UITableView:
Just #import the UIImageView+WebCache.h header, and call the setImageWithURL: method from the
tableView:cellForRowAtIndexPath: UITableViewDataSource method. Everything will be handled for you,
from parallel downloads to caching management. If you assigned an image to the view (via the
`images` property), this image will be used as a placeholder, waiting for the web image to be
loaded.
- Place an UIImageView as a subview of your UITableViewCell in Interface Builder
- Set its class to SDImageView in the identity panel.
- Optionally set an image from your bundle to this UIImageView, it will be used as a placeholder
image waiting for the real image to be downloaded.
- In your tableView:cellForRowAtIndexPath: UITableViewDataSource method, invoke the setImageWithURL:
method of the SDWebImage view with the URL of the image to download
#import "UIImageView+WebCache.h"
Your done, everything will be handled for you, from parallel downloads to caching management.
...
- (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 set the placeholder image
cell.imageView.image = [UIImage imageNamed:@"placeholder.png"];
}
// 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"]];
cell.textLabel.text = @"My Text";
return cell;
}
### Asynchronous Image Downloader
... ...
... ... @@ -8,15 +8,8 @@
#import <UIKit/UIKit.h>
@class SDWebImageDownloader;
@interface SDWebImageView : UIImageView
{
UIImage *placeHolderImage;
SDWebImageDownloader *downloader;
}
@interface UIImageView (WebCache)
- (void)setImageWithURL:(NSURL *)url;
- (void)downloadFinishedWithImage:(UIImage *)image;
@end
\ No newline at end of file
@end
... ...
/*
* This file is part of the SDWebImage package.
* (c) Olivier Poitrey <rs@dailymotion.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
#import "UIImageView+WebCache.h"
#import "UIImageViewHelper.h"
@implementation UIImageView (WebCache)
- (void)setImageWithURL:(NSURL *)url
{
UIImageViewHelper *helper = nil;
if ([self.subviews count] > 0)
{
helper = [self.subviews objectAtIndex:0];
}
if (helper == nil)
{
helper = [[[UIImageViewHelper alloc] initWithDelegate:self] autorelease];
[self addSubview:helper];
}
// Remove in progress downloader from queue
[helper cancel];
// Save the placeholder image in order to re-apply it when view is reused
if (helper.placeHolderImage == nil)
{
helper.placeHolderImage = self.image;
}
else
{
self.image = helper.placeHolderImage;
}
UIImage *cachedImage = [helper imageWithURL:url];
if (cachedImage)
{
self.image = cachedImage;
}
else
{
[helper downloadWithURL:url];
}
}
@end
\ No newline at end of file
... ...
/*
* This file is part of the SDWebImage package.
* (c) Olivier Poitrey <rs@dailymotion.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
#import <UIKit/UIKit.h>
#import "SDWebImageDownloader.h"
@interface UIImageViewHelper : UIView
{
UIImageView *delegate;
SDWebImageDownloader *downloader;
UIImage *placeHolderImage;
}
@property (nonatomic, retain) UIImage *placeHolderImage;
- (id)initWithDelegate:(UIImageView *)aDelegate;
- (UIImage *)imageWithURL:(NSURL *)url;
- (void)downloadWithURL:(NSURL *)url;
- (void)cancel;
@end
... ...
... ... @@ -6,64 +6,51 @@
* file that was distributed with this source code.
*/
#import "SDWebImageView.h"
#import "UIImageViewHelper.h"
#import "SDImageCache.h"
#import "SDWebImageDownloader.h"
@implementation SDWebImageView
@implementation UIImageViewHelper
- (void)dealloc
{
[placeHolderImage release];
[downloader release];
[super dealloc];
}
@synthesize placeHolderImage;
#pragma mark RemoteImageView
- (void)setImageWithURL:(NSURL *)url
- (id)initWithDelegate:(UIImageView *)aDelegate
{
if (downloader != nil)
if (self = [super init])
{
// Remove in progress downloader from queue
[downloader cancel];
[downloader release];
downloader = nil;
delegate = aDelegate;
self.hidden = YES;
}
return self;
}
// Save the placeholder image in order to re-apply it when view is reused
if (placeHolderImage == nil)
{
placeHolderImage = [self.image retain];
}
else
{
self.image = placeHolderImage;
}
- (UIImage *)imageWithURL:(NSURL *)url
{
return [[SDImageCache sharedImageCache] imageFromKey:[url absoluteString]];
}
UIImage *cachedImage = [[SDImageCache sharedImageCache] imageFromKey:[url absoluteString]];
- (void)downloadWithURL:(NSURL *)url
{
downloader = [[SDWebImageDownloader downloaderWithURL:url target:self action:@selector(downloadFinishedWithImage:)] retain];
}
if (cachedImage)
{
self.image = cachedImage;
}
else
{
downloader = [[SDWebImageDownloader downloaderWithURL:url target:self action:@selector(downloadFinishedWithImage:)] retain];
}
- (void)cancel
{
[downloader cancel];
[downloader release];
downloader = nil;
}
- (void)downloadFinishedWithImage:(UIImage *)anImage
{
// Apply image to the underlaying UIImageView
self.image = anImage;
delegate.image = anImage;
// Store the image in the cache
[[SDImageCache sharedImageCache] storeImage:anImage forKey:[downloader.url absoluteString]];
// Free the downloader
[downloader release];
downloader = nil;
}
@end
@end
\ No newline at end of file
... ...