延迟加载tableview中的图像 [英] lazy loading images in tableview

查看:128
本文介绍了延迟加载tableview中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是----在<$ c中加载数据时,我在自定义单元格中使用 UITableView $ c> tableview 它正在加载描述和标题也是日期但不在tableview中加载图像。有时会将所有图像加载到一行中。当我使用此代码滚动工作正常但不加载 TableView 中的图像。我想要延迟加载概念。转到下面的链接。

My problem is ---- I am using UITableView in custom cell when load data in tableview it is loading description and title also date but not load images in tableview.some times load all images in one row. When i am using this code scroll is working good but not load images in TableView. I want lazy load concepts. go to below link.

 - (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;
   }


推荐答案

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;


        });
    });

这篇关于延迟加载tableview中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆