2014-05-12 104 views
1

我的问题是----我在custom cell中使用UITableView当加载数据在tableview它是加载描述和标题也日期但没有加载图像tableview.some次加载所有图像在一行。当我使用此代码滚动工作正常,但不加载图像在TableView。我想要延迟加载概念。去下面的链接。在tableview中延迟加载图像

enter image description here

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    newsobject=(newscustamCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (newsobject ==nil) 
    { 
     [[NSBundle mainBundle]loadNibNamed:@"newscustamCell" owner:self options:nil]; 
    } 
    dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 
    dispatch_async(q, ^{ 

     NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[newsarry objectAtIndex:indexPath.row] objectForKey:@"image_file"]]]; 

     /* Fetch the image from the server... */ 

     NSData *data = [NSData dataWithContentsOfURL:url]; 
     UIImage *img = [[UIImage alloc] initWithData:data]; 

     dispatch_async(dispatch_get_main_queue(), ^{ 

      /* This is the main thread again, where we set the tableView's image to 
     be what we just fetched. */ 

     newsobject.allnewsimg.image = img; 
    // newsobject.allnewsimg.image=[UIImage imageWithData:data]; 

     }); 

    }); 

     newsobject.allnewsdes.text=[[[newsarry objectAtIndex:indexPath.row]   objectForKey:@"long_desc"]stringByStrippingTags]; 

     newsobject.allnewslabel.text=[[newsarry objectAtIndex:indexPath.row]  objectForKey:@"title"]; 
    ![enter image description here][1] 
     newsobject.timelabel.text=[[newsarry objectAtIndex:indexPath.row] objectForKey:@"date"]; 

    return newsobject; 
    } 

回答

0

我将分享你一些代码。请遵循此代码。

首先,您必须从服务器检索所有数据。之后在tableview加载。

试试这个代码:

- (void)viewDidLoad 
{  
    NSString *[email protected]"http://put your url"; 
    self.responseData=[NSMutableData data]; 
    NSURLConnection *connection=[[NSURLConnection alloc]initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] delegate:self]; 
    NSLog(@"connection====%@",connection); 
} 

JSON委托方法-------------

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    [self.responseData setLength:0]; 
} 
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [self.responseData appendData:data]; 
} 
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    self.responseData=nil; 
} 
-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSArray *response_Array = [NSJSONSerialization JSONObjectWithData:self.responseRankData options:kNilOptions error:nil]; 

    NSDictionary *dataDictionary=nil; 
    NSDictionary *imgurlDic=nil; 
    NSArray *imgDic=nil; 

    // I am retrieve image according to my url. So please edit this according to your url. i am just write the format. 

    for (int i=0; i < response_Array.count; i++) 
    { 
     imgurlDic = [imgDic objectAtIndex:i]; 
     imgDic = [dataDictionary objectForKey:@"imageKey"]; // Take your Key according to URL 

     NSURL *url = [NSURL URLWithString:[imgurlDic objectForKey:@"url"]]; 
     NSData *data = [NSData dataWithContentsOfURL:url]; 
     UIImage *img = [[UIImage alloc] initWithData:data]; 
     [self.Img_Array addObject:img]; // Adding all image in your image Array. 

     NSLog(@"imgurlDic == %@",imgurlDic); 
    } 

    [self.tableview reloadData]; // reload your tableview here. 
} 

让所有图像阵列中,你可以使用这个数组在您的cellForRowAtIndexPath方法或您想要加载图像的位置。

+1

的NSData *数据= [NSData的dataWithContentsOfURL:URL];如果图像数据较大,它将阻止用户界面。它不是懒加载的概念。 – Pawan

+0

我从URL检索480x1024大小的图像,并显示在我的tableview单元格,其中frameize只有40x40。所以我认为会的。 –

+0

问题是关于延迟加载的概念,你可以用后台线程做同样的事情。这并不会阻止你的用户界面。 – Pawan

1

您可以使用一些LibSDWebImageAfnetworkingLibs支持延迟加载图像并自动缓存此图像。 所有你需要做的:

[cell.imageView setImageWithURL:[NSURL URLWithString:@"url of image"] 
        placeholderImage:nil]; 
+0

谢谢你回答我,我已经这样做,我从服务器获取图像,但滚动非常缓慢,它的击中。 – venky

+0

嗨,我看到你的例子程序,但它的工作给我,但滚动非常缓慢,并转移到结果。 – venky

+0

我从服务器获取所有数据,并将对象添加到newsarray对象的newsarray数据中。当我运行这个程序所有数据dislay tablview(图像和长描述和数据和标题也)但当我滚动tablview它非常缓慢,并移动到结果因为图像加载lazy.how我可以解决我的问题... – venky

0

的因为该表视图的细胞迅速排队的方法/再利用,通过你的网络的要求完成加载,你指的是电池的时间可能是不一样的一个你认为它是(因为它离开屏幕并被排队,然后重新分配到不同的indexPath)。此外,在返回单元格之前,您需要确保将imageViewimage属性重置为nil或某个默认值。请参阅我的答案以获取更多信息:https://stackoverflow.com/a/23473911/91458

此外 - 最好保留已检索图像的缓存。检查类NSCache类,这是非常类似于使用可变字典,但具有内置内存管理,等等。

0

我也使用了这个技巧,但线程和索引之间的线程弄得乱七八糟,所以我搜索,我发现GitHub上的一个伟大的图书馆。

你只需要#import <SDWebImage/UIImageView+WebCache.h>到你的项目,你也可以定义占位符,当图像被与眼前这个代码下载:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     static NSString *CellIdentifier; 
     CustomCell *cell = [tableview dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

     MyObject *object = (MyObject *)[self.list.array objectAtIndex:indexPath.row]; 

     [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] 
         placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; 

     return cell; 
} 

它还高速缓存下载图像,并给你很好的表现。

希望它能帮助你!

+0

嗨,我下载并传播此框架,但我得到了一个错误。即找不到#import 文件。 – venky

+0

@ user3627091你是通过Cocoa Pods来安装它,还是你手动将文件夹复制到你的项目中。 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString * CellIdentifier = @“Cell”;如果您执行了第二个操作,则需要导入UIImageView + WebCache.h – EridB

0

我希望这个链接对你有帮助。通过使用异步类图像加载异步...

https://github.com/nicklockwood/AsyncImageView

+0

- (UITableViewCell *)。 newsobject =(newscustamCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; – venky

+0

if(newsobject == nil) {[NSBundle mainBundle] loadNibNamed:@“newscustamCell”owner:self options:nil];您可以使用下面的代码来创建一个新的URL:{“newsarry objectAtIndex:indexPath.row”objectForKey:@“image_file”]]]; – venky

+0

NSData * data = [NSData dataWithContentsOfURL:url]; newsobject.allnewsimg.image = [UIImage imageWithData:data]; newsobject.allnewsdes.text = [[[newsarry objectAtIndex:indexPath.row] objectForKey:@“long_desc”] stringByStrippingTags]; – venky

4
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 
      dispatch_async(queue, ^{ 

       NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[newsarry objectAtIndex:indexPath.row] objectForKey:@"image_file"]]]; 

       NSData *data = [NSData dataWithContentsOfURL:url]; 
       UIImage *image = [UIImage imageWithData:data]; 

       dispatch_async(dispatch_get_main_queue(), ^{ 

      [tableView cellForRowAtIndexPath:indexPath].imageView.image = image; 


     }); 
    }); 
+0

当我使用这些代码我可以解决图像懒加载问题... – venky