/PTThumbnailLoader

Async image loader

Primary LanguageObjective-C

Async image loader

Using PTThumbnailLoader

Add files into your project

Import class

import "PTThumbnailLoader.h"

Load image into UITableView

// load image

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // store downloaded file in cache

	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *cachePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Thumbnails"];
	NSString *filePath = [cachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png", article.identifier]];

	// download image

   	PTThumbnailLoader *loader = [[PTThumbnailLoader alloc] init];
	loader.url = article.imageURL;
	loader.indexPath = indexPath;
	loader.cacheFile = filePath;
	loader.delegate = self;

	[loader startDownload];
}

// add downloaded image into cell

- (void)thumbnailLoader:(PTThumbnailLoader *)loader didLoad:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [itemsView cellForRowAtIndexPath:indexPath];
    UIImageView *imageView = (UIImageView *)[cell viewWithTag:1];
    imageView.image = loader.image;

    [loader release];
}

- (void)thumbnailLoader:(PTThumbnailLoader *)loader didFail:(NSError *)error {
    [loader release];
}

Using blocks

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:@"ID"];
	if (cell == nil) cell = [self reuseTableViewCellWithIdentifier:@"ID"];

	Gallery *gallery = [items objectAtIndex:indexPath.row];
	UIImageView *imageView = (UIImageView *)[cell.contentView viewWithTag:1];

	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   	NSString *cachePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Thumbnails"];
 	NSString *filePath = [cachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png", gallery.identifier]];
    
 	PTThumbnailLoader *loader = [[PTThumbnailLoader alloc] init];
    loader.cacheFile = filePath;
    
    PTThumbnailLoaderResultBlock resultBlock = ^(UIImage *image) {
        gallery.preview = image;
        imageView.image = gallery.preview;
    };
    
    PTThumbnailLoaderFailureBlock failureBlock = ^(NSError *error) {
        NSLog(@"ERROR %@", [error localizedDescription]);
    };
    
    [loader downloadImage:gallery.imageURL withResultBlock:resultBlock failureBlock:failureBlock];
    [loader release];

	return cell;
}