Reputation: 4265
I currently have an array with lots of image URLs called images
They are being put into my table like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSLog(@"indexPath.row=%ld", (long)indexPath.row);
NSLog(@"images[indexPath.row]=%@", [images objectAtIndex:indexPath.row]);
NSString *stringy = @"http://www.tragicclothing.co.uk/";
NSString *link = [stringy stringByAppendingString: images[indexPath.row]];
NSString* encodedString = [link stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:encodedString]];
cell.thumbnailImageView.image = [UIImage imageWithData: imageData];
cell.backgroundColor = [UIColor clearColor];
cell.opaque = NO;
cell.backgroundView = nil;
return cell;
}
But this is causing a lot of lag when scrolling. How could I cache the data? to prevent this lag? I have seen SDWebImage but I don't quite know how to use it! It seems very complicated.
Should I be using
SDImageCache *imageCache = [[SDImageCache alloc] initWithNamespace:@"myNamespace"];
[imageCache queryDiskCacheForKey:myCacheKey done:^(UIImage *image)
{
// image is not nil if image was found
}];
Upvotes: 0
Views: 4169
Reputation: 45500
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSLog(@"indexPath.row=%ld", (long)indexPath.row);
NSLog(@"images[indexPath.row + 7]=%@", [images objectAtIndex:indexPath.row + 7]);
NSString *stringy = @"http://www.tragicclothing.co.uk/";
NSString *link = [stringy stringByAppendingString: images[indexPath.row + 7]];
NSString* encodedString = [link stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[cell.thumbnailImageView setImageWithURL:[NSURL URLWithString:encodedString] placeholderImage:[UIImage imageNamed:@"Comp 2_00000.png"]];
cell.backgroundColor = [UIColor clearColor];
cell.opaque = NO;
cell.backgroundView = nil;
return cell;
}
Upvotes: 1
Reputation: 3444
nothing can be simple then SDWebImage
it prodide the following solution
An UIImageView category adding web image and cache management to the Cocoa Touch framework
An asynchronous image downloader
An asynchronous memory + disk image caching with automatic cache expiration handling
#import <SDWebImage/UIImageView+WebCache.h>
NSString* encodedString = [link stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[cell.thumbnailImageView setImageWithURL:[NSURL URLWithString:encodedString]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
Upvotes: 1