我目前有一个包含大量图像网址的数组,名为images
它们会被放入我的表格中:
- (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;
}
但这会在滚动时造成很多延迟。我怎么能缓存数据?防止这种滞后?我见过SDWebImage但我不太清楚如何使用它!这看起来很复杂。
我应该使用
SDImageCache *imageCache = [[SDImageCache alloc] initWithNamespace:@"myNamespace"];
[imageCache queryDiskCacheForKey:myCacheKey done:^(UIImage *image)
{
// image is not nil if image was found
}];
答案 0 :(得分:1)
没有什么比SDWebImage
更简单它促进了以下解决方案
UIImageView类别,将Web图像和缓存管理添加到Cocoa Touch框架
异步图片下载器
具有自动缓存到期处理的异步内存+磁盘映像缓存
#import <SDWebImage/UIImageView+WebCache.h>
NSString* encodedString = [link stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[cell.thumbnailImageView setImageWithURL:[NSURL URLWithString:encodedString]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
答案 1 :(得分:1)
- (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;
}