在UITableView iPhone SDK中延迟加载图像
我需要为我的tableview中的图像实现延迟加载的概念,以便最初向用户提供文本数据,然后再向用户提供图像 如何在我的应用程序中实现此功能。。需要帮助。。请 提前谢谢 施宾你是这个意思吗在UITableView iPhone SDK中延迟加载图像,iphone,image,lazy-loading,Iphone,Image,Lazy Loading,我需要为我的tableview中的图像实现延迟加载的概念,以便最初向用户提供文本数据,然后再向用户提供图像 如何在我的应用程序中实现此功能。。需要帮助。。请 提前谢谢 施宾你是这个意思吗 我为我的一个项目所做的工作如下:; 通过“UITableViewCell+Async.h”中的类别扩展UITableViewCell类 (如果您不确定Obj C中的类别是什么,请参阅一些示例) 然后在实现文件“UITableViewCell+Async.m”中 这基本上为UITableViewCell添加了一项
我为我的一个项目所做的工作如下:; 通过“UITableViewCell+Async.h”中的类别扩展UITableViewCell类 (如果您不确定Obj C中的类别是什么,请参阅一些示例) 然后在实现文件“UITableViewCell+Async.m”中 这基本上为UITableViewCell添加了一项功能,以在新的背景线程中加载图像,调整图像大小并将其设置为imageview。添加了检查标记,以查看单元格是否仍在等待图像,因为它可以重复使用,图像的另一个线程可能正在为该重复使用的单元格下载另一个图像 上述代码中的函数,签名为
+(UIImage*)getImageResized:(NSString*)url inSize:(CGSize)size;
检查图像的本地缓存,从web下载图像(如果不在缓存中),将其保存到本地缓存,按给定大小调整图像大小并返回图像,所有操作都通过同步(阻止)方法调用完成。由于这已经是后台线程,因此阻止它执行此操作不会有任何伤害。当该方法返回图像时,如果该图像仍然具有相同的标记(不用于其他行),则将其设置为单元格的imageview
在cellForRowAtIndexPath方法中,您可以添加新类别,并且您应该这样做
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"......."];
if (cell == nil) {
................
}
................
[cell loadAsyncImage:deal.logo withIndex:indexPath.row inWidth:40 inHeight:40];
................
return cell;
}
我使用了一个用于延迟加载的网络队列 在.h文件中
#import "ASINetworkQueue.h"
并定义一个变量
//array contains url address of images and a variable of ASINetworkQueue;
NSMutableArray *arrImg;
ASINetworkQueue *queue;
在.m文件中
static int rowNo = 0;
- (void)downloadImages{
if(queue == nil){
queue = [[[ASINetworkQueue alloc] init] autorelease];
}
for(NSString* urlString in arrImg)
{
NSURL *url = [NSURL URLWithString:urlString];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request setDidFinishSelector:@selector(requestDone:)];
[request setDidFailSelector:@selector(requestWentWrong:)];
[queue addOperation:request];
}
[queue go];
}
- (void)requestDone:(ASIHTTPRequest*)req{
for (int i = 0; i < [arrImg count]; i++) {
NSMutableString *d = [[NSMutableString alloc] init];
d = [arrImg objectAtIndex:i];
//comparing the url to get correct row no of cell
if([[req.url absoluteString] isEqualToString:d]){
rowNo = i;
break;
}
}
//creating a cell with rowNo
NSIndexPath *index = [NSIndexPath indexPathForRow:rowNo inSection:0];
UITableViewCell *cell = (UITableViewCell *)[roomTable cellForRowAtIndexPath:index];
UIImageView *v = (UIImageView *)[cell.contentView viewWithTag:10];
v.image = [UIImage imageWithData:[req responseData]];
[cell release];
cell = nil;
[v release];
v = nil;
}
- (void)requestWentWrong:(ASIHTTPRequest*)req{
for (int i = 0; i < [arrImg count]; i++) {
NSMutableString *d = [[NSMutableString alloc] init];
d = [arrImg objectAtIndex:i];
//comparing the url to get correct row no of cell
if([[req.url absoluteString] isEqualToString:d]){
rowNo = i;
break;
}
}
NSLog(@"No data for cell : %d",rowNo);
NSLog(@"Request returned an error %@",[req error]);
}
static int rowNo=0;
-(无效)下载图像{
如果(队列==nil){
队列=[[ASINetworkQueue alloc]init]autorelease];
}
for(在arrImg中为NSString*URLSING)
{
NSURL*url=[NSURL URLWithString:urlString];
ASIHTTPRequest*request=[AsiHttpRequestWithURL:url];
[请求setDelegate:self];
[请求setDidFinishSelector:@selector(请求完成:)];
[RequestSetDidFailSelector:@selector(RequestWentRong:)];
[队列添加操作:请求];
}
[排队走];
}
-(void)requestDone:(ASIHTTPRequest*)请求{
对于(int i=0;i<[arrImg count];i++){
NSMutableString*d=[[NSMutableString alloc]init];
d=[arrimmg objectAtIndex:i];
//比较url以获取单元格的正确行号
如果([[req.url absoluteString]IsequalString:d]){
rowNo=i;
打破
}
}
//创建具有行号的单元格
NSIndexPath*索引=[NSIndexPath indexPathForRow:rowNo分类:0];
UITableViewCell*单元格=(UITableViewCell*)[RowAtIndexPath:索引的roomTable单元格];
UIImageView*v=(UIImageView*)[cell.contentView视图带标记:10];
v、 图像=[UIImage imageWithData:[req responseData]];
[细胞释放];
细胞=无;
[v释放];
v=零;
}
-(void)requestWentWrong:(ASIHTTPRequest*)请求{
对于(int i=0;i<[arrImg count];i++){
NSMutableString*d=[[NSMutableString alloc]init];
d=[arrimmg objectAtIndex:i];
//比较url以获取单元格的正确行号
如果([[req.url absoluteString]IsequalString:d]){
rowNo=i;
打破
}
}
NSLog(@“单元格%d没有数据”,行号);
NSLog(@“请求返回错误%@”,[req error]);
}
谢谢。。我得到了这个,我把IconDownloader添加到我的项目中。但是NSURLConnection:didReceiveData委托方法不起作用。所以决定尝试另一种方法。你能帮我解决代表的问题吗?@Shibin:听起来像是一个新问题,用“提问”按钮创建一个新问题,而不是在评论中寻求帮助。问题已经发布了NSURLConnection:didReceiveData委托
如果连接在主线程上实现,则调用该方法。重复:请参阅EGOImageLoading,虽然我们感谢您在本文中付出的努力,但请不要将其复制到每个适当的问题上。试着精心设计每个具体问题的答案。谢谢
//array contains url address of images and a variable of ASINetworkQueue;
NSMutableArray *arrImg;
ASINetworkQueue *queue;
static int rowNo = 0;
- (void)downloadImages{
if(queue == nil){
queue = [[[ASINetworkQueue alloc] init] autorelease];
}
for(NSString* urlString in arrImg)
{
NSURL *url = [NSURL URLWithString:urlString];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request setDidFinishSelector:@selector(requestDone:)];
[request setDidFailSelector:@selector(requestWentWrong:)];
[queue addOperation:request];
}
[queue go];
}
- (void)requestDone:(ASIHTTPRequest*)req{
for (int i = 0; i < [arrImg count]; i++) {
NSMutableString *d = [[NSMutableString alloc] init];
d = [arrImg objectAtIndex:i];
//comparing the url to get correct row no of cell
if([[req.url absoluteString] isEqualToString:d]){
rowNo = i;
break;
}
}
//creating a cell with rowNo
NSIndexPath *index = [NSIndexPath indexPathForRow:rowNo inSection:0];
UITableViewCell *cell = (UITableViewCell *)[roomTable cellForRowAtIndexPath:index];
UIImageView *v = (UIImageView *)[cell.contentView viewWithTag:10];
v.image = [UIImage imageWithData:[req responseData]];
[cell release];
cell = nil;
[v release];
v = nil;
}
- (void)requestWentWrong:(ASIHTTPRequest*)req{
for (int i = 0; i < [arrImg count]; i++) {
NSMutableString *d = [[NSMutableString alloc] init];
d = [arrImg objectAtIndex:i];
//comparing the url to get correct row no of cell
if([[req.url absoluteString] isEqualToString:d]){
rowNo = i;
break;
}
}
NSLog(@"No data for cell : %d",rowNo);
NSLog(@"Request returned an error %@",[req error]);
}