如何在ios上缓存图像
我目前有一个数组,其中包含许多名为如何在ios上缓存图像,ios,objective-c,url,caching,Ios,Objective C,Url,Caching,我目前有一个数组,其中包含许多名为images的图像URL,它们被放入我的表中,如下所示: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *simpleTableIdentifier = @"SimpleTableCell"; SimpleTableCell *cell = (Simp
images
的图像URL,它们被放入我的表中,如下所示:
- (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;
}
但这会在滚动时造成很大的延迟。如何缓存数据?为了防止这种滞后?我见过,但我不太知道如何使用它!看起来很复杂
我应该用吗
SDImageCache *imageCache = [[SDImageCache alloc] initWithNamespace:@"myNamespace"];
[imageCache queryDiskCacheForKey:myCacheKey done:^(UIImage *image)
{
// image is not nil if image was found
}];
没有什么是简单的 它产生了以下解决方案 向Cocoa Touch框架添加web映像和缓存管理的UIImageView类别 异步图像下载程序 具有自动缓存过期处理的异步内存+磁盘映像缓存
#import <SDWebImage/UIImageView+WebCache.h>
NSString* encodedString = [link stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[cell.thumbnailImageView setImageWithURL:[NSURL URLWithString:encodedString]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
#导入
NSString*encodedString=[link stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[cell.thumbnailImageView setImageWithURL:[NSURL URLWithString:encodedString]
占位符图像:[UIImage ImageName:@“placeholder.png”];
您将SDWebImage称为复杂?你还想要什么?只需调用方法[myImage setImage…”。。。。[myImage setImage…”延迟不仅仅是滚动造成的。你正在同步下载图像,这是你的问题!@Cyrille我如何一次下载所有图像。我想做一种无限滚动类型的东西…你不需要一次下载所有图像,让图像异步下载。+1显然,带有\importUIImageView
类别的code>。谢谢rob!我会在有机会时添加导入内容(现在在手机上:-s)
- (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;
}