使用url中的uitableview缓存UIImage [英] Cache UIImage using uitableview from url
问题描述
我有一个uitableview,其中包含两个UImage的自定义单元格.徽标图像来自在线网站,这就是为什么需要缓存图像的原因.像现在这样加载图像:
I hava a uitableview , with custom cell containing two UImages. The logo images are taken from an online website, that's why there's a need to cache the images. Loading the image till now is made like this :
NSURL * imageURL = [NSURL URLWithString:[arra1 objectAtIndex:indexPath.row / 2]];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
NSURL * imageURL2 = [NSURL URLWithString:[arra2 objectAtIndex:indexPath.row / 2]];
NSData * imageData2 = [NSData dataWithContentsOfURL:imageURL2];
cell.ima1.image = [UIImage imageWithData:imageData];
cell.ima2.image2 = [UIImage imageWithData:imageData2];
我从搜索中学到的是dataWithContentsOfURL不是异步的,滚动时会花费很多时间.我尝试了几种方法,但我似乎找不到正确的方法.这是我第一次缓存UIImages,我非常感谢实现的详细说明,因此我可以从完成工作中学习.非常感谢
What i learned from searching , is that dataWithContentsOfURL is not asynchronous , and while scrolling it will take a lot of time. I tried several methods but i can't seem to get to right one. This is my first time caching UIImages , i would highly appreciate a detailed explanation with implementation so i could learn aside from getting the job done. Many Thanks
推荐答案
我使用的是完美的库
您只需要将 #import< SDWebImage/UIImageView + WebCache.h>
导入项目,并且还可以在仅使用以下代码下载图像时定义占位符:
You just need to #import <SDWebImage/UIImageView+WebCache.h>
to your project, and you can define also the placeholder when image is being downloaded with just this code:
- (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 use the new provided setImageWithURL: method to load the web image
[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
cell.textLabel.text = @"My Text";
return cell;
}
它还缓存下载的图像,并为您提供出色的性能.
It also cache downloaded images and gives you great performance.
希望它会对您有所帮助!
Hope it will help you!
这篇关于使用url中的uitableview缓存UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!