2013-05-13 116 views
0

我试图从服务器上下载一些缩略图,然后在每个单元格中显示它们之后缓慢:UITableView的滚动下载细胞图像

// displaying image... 
dictionary = [newsFeed objectAtIndex:indexPath.row]; 
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[dictionary objectForKey:@"image"]]]]; 
cell.imageView.image = [image imageScaledToSize:CGSizeMake(55, 55)];  

但慢慢地下载和显示图像,表视图滚动后!

+0

您应该使用dequeueReusableCellWithIdentifier: – rocky 2013-05-13 21:03:41

+0

我没有,但我并没有把它写入上述代码,我编辑我的代码的方式 – 2013-05-13 21:08:16

回答

3

主要问题是[NSData dataWithContentsOfURL:]方法是同步请求。它阻塞正在运行的线程,直到请求完成。

如果您在主线程上运行此操作,则应避免此类交互。此线程将被冻结,用户将感到失望;)

您有许多解决方案。一个简单的方法是使用第三个库,如SDWebImageAFNetworking

两者都有类别UIImageView类,允许以简单的方式使用它们。例如,使用UIImageView+AFNetworking你应该做如下所示:

[cell.imageView setImageWithURL:yourURL placeholderImage:yourPlaceholderImage]; 
+0

非常感谢,但图片不会出现,直到我滚动thable,如何解决这个问题? – 2013-05-14 08:36:27

+0

@ Mc.Lover你能分享一些代码吗?谢谢。 – 2013-05-14 09:28:17

0

使用AFNetworking会给你一个更好的感觉。有一个UIImageView类别作为项目的一部分,它将插入临时图像,同时切换到后台线程以下载实际图像。

这将保持图像下载锁定主线程。一旦下载图像,临时图像就会替换掉。

下面是一个很好的教程,其中包含有关使用UIImageView类别AFNetworking crash course的一些指示。

0

我已经写在这里的答案:

https://stackoverflow.com/a/14624875/1375695

这也解释了如何使用大中央寄发使您的表滚动顺畅,同时您的图像正在后台下载。该答案不要求使用第三方库。虽然 - 正如Andrew和Flexaddicted提到的那样 - 有些库会为您提供解决方案 - 可能值得阅读,这样您就可以理解底层需要发生什么。

注意,在我提出的解决方案,你仍然可以使用你的同步方法实际上是从网络获取数据:

UIImage *image = [UIImage imageWithData: 
         [NSData dataWithContentsOfURL: 
         [NSURL URLWithString: 
         [dictionary objectForKey:@"image"]]]]; 

因为那将是一个异步后台线程调用。

0

我强烈建议使用@flexaddicted推荐的AFNetworking框架(UIImageView+AFNetworking)。另外,请查看Sensible TableView框架。它应该能够自动从Web服务获取所有数据并将其显示在表格视图中,并且还会自动处理所有异步图像下载。应该为你节省一堆工作。

2

我想你是想说这个。

NSURL* url = [NSURL URLWithString:@"http://www.YourImageUrl.com"]; 
NSURLRequest* request = [NSURLRequest requestWithURL:url]; 


[NSURLConnection sendAsynchronousRequest:request 
     queue:[NSOperationQueue mainQueue] 
     completionHandler:^(NSURLResponse * response, 
      NSData * data, 
      NSError * error) { 
    if (!error){ 
      UIImage* image = [[UIImage alloc] initWithData:data]; 
     // Now workout with the image 
    } 

}]; 

这将使异步调用,并在tableview加载后加载图像i。e当图像加载完成时,图像将显示,但是当需要加载表格视图时将加载表格。

1

直接使用这样

UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[dictionary objectForKey:@"image"]]]]; 

它采用主线程,所以你不能滚动befor下载您Image.So你加NSURLConnectionDataDelegate,NSURLConnectionDelegate通过

插入添加此协议,并下载图像这些代码行您想要下载您的图像

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]; 
    NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self]; 
    received_data = [[NSMutableData alloc]init]; 
    [connection start]; 

这些行触发这些委托下载数据。

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    [received_data setLength:0]; 
} 

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [received_data appendData:data]; 

} 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    //Use your download image 
    imageView.image = [UIImage imageWithData:received_data];  
}