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

This commit is contained in:
Olivier Poitrey 2009-09-23 03:05:40 +02:00
parent b2a3d31557
commit 972c304957
6 changed files with 169 additions and 89 deletions

View File

@ -1,8 +1,7 @@
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

View File

@ -1,69 +0,0 @@
/*
* 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 "SDWebImageView.h"
#import "SDImageCache.h"
#import "SDWebImageDownloader.h"
@implementation SDWebImageView
- (void)dealloc
{
[placeHolderImage release];
[downloader release];
[super dealloc];
}
#pragma mark RemoteImageView
- (void)setImageWithURL:(NSURL *)url
{
if (downloader != nil)
{
// Remove in progress downloader from queue
[downloader cancel];
[downloader release];
downloader = nil;
}
// 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 *cachedImage = [[SDImageCache sharedImageCache] imageFromKey:[url absoluteString]];
if (cachedImage)
{
self.image = cachedImage;
}
else
{
downloader = [[SDWebImageDownloader downloaderWithURL:url target:self action:@selector(downloadFinishedWithImage:)] retain];
}
}
- (void)downloadFinishedWithImage:(UIImage *)anImage
{
// Apply image to the underlaying UIImageView
self.image = anImage;
// Store the image in the cache
[[SDImageCache sharedImageCache] storeImage:anImage forKey:[downloader.url absoluteString]];
// Free the downloader
[downloader release];
downloader = nil;
}
@end

View File

@ -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
@end

54
UIImageView+WebCache.m Normal file
View File

@ -0,0 +1,54 @@
/*
* 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

26
UIImageViewHelper.h Normal file
View File

@ -0,0 +1,26 @@
/*
* 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

56
UIImageViewHelper.m Normal file
View File

@ -0,0 +1,56 @@
/*
* 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 "UIImageViewHelper.h"
#import "SDImageCache.h"
@implementation UIImageViewHelper
@synthesize placeHolderImage;
- (id)initWithDelegate:(UIImageView *)aDelegate
{
if (self = [super init])
{
delegate = aDelegate;
self.hidden = YES;
}
return self;
}
- (UIImage *)imageWithURL:(NSURL *)url
{
return [[SDImageCache sharedImageCache] imageFromKey:[url absoluteString]];
}
- (void)downloadWithURL:(NSURL *)url
{
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
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