首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >解析XML -从URL加载图像

解析XML -从URL加载图像
EN

Stack Overflow用户
提问于 2013-01-21 20:17:00
回答 3查看 1.3K关注 0票数 2

我正在解析一个包含图片链接的XML文件,并尝试在UITableView中显示它们。由于某种原因,它们没有出现在视图中。我不认为这个问题与我的解析脚本有任何关系,因为它可以在表中显示文本。下面是我显示图片的代码:

代码语言:javascript
运行
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
    static NSString *CellIdentifier = @"Cell";

     Tweet *currentTweet = [[xmlParser tweets] objectAtIndex:indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)

    {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];


        CGRect imageFrame = CGRectMake(2, 8, 40, 40);

        UIImageView *customImage = [[UIImageView alloc] initWithFrame:imageFrame];

        customImage.tag = 0013;

        [cell.contentView addSubview:customImage];


    }

    UIImageView *customImage = (UIImageView *)[cell.contentView viewWithTag:0013];

    customImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[currentTweet pic]]]];

    return cell;

}

我可能需要先加载图片吗?如果是这样的话,我如何才能做到这一点,因为我只在解析xml之后才获得图像的urls?

感谢任何帮助,Antoine

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-01-21 20:42:36

代码语言:javascript
运行
复制
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    Tweet *currentTweet = [[xmlParser tweets] objectAtIndex:indexPath.row];
    UIImageView *customImage = (UIImageView*)[cell.contentView viewWithTag:1234];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

        CGRect imageFrame = CGRectMake(2, 8, 40, 40);
        customImage = [[UIImageView alloc] initWithFrame:imageFrame];
        customImage.tag = 1234;
        [cell.contentView addSubview:customImage];
    }

    dispatch_queue_t imageQueue = dispatch_queue_create("queue", NULL);
    dispatch_async(imageQueue, ^{
        UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[currentTweet pic]]]];
        dispatch_async(dispatch_get_main_queue(), ^{
            customImage.image = image;
        });
    });

    return cell;
}
票数 2
EN

Stack Overflow用户

发布于 2013-01-21 20:24:04

您需要在表视图中异步加载图像,遵循本教程将有所帮助

http://dbrajkovic.wordpress.com/2012/01/08/load-images-asynchronously-in-a-uitableview-using-gcd-grand-central-dispatch/

http://kosmaczewski.net/asynchronous-loading-of-images-in-a-uitableview/

票数 1
EN

Stack Overflow用户

发布于 2013-01-21 20:35:25

代码语言:javascript
运行
复制
 @interface ViewController ()
{
    NSMutableArray *images;
NSMutableDictionary *dicImages_msg;
    BOOL isDecliring_msg;
    BOOL isDragging_msg;

  }
 @implementation ViewController ()
 - (void)viewDidLoad
  {
    [super viewDidLoad];
    dicImages_msg = [[NSMutableDictionary alloc] init];
    [self parsing];//parse and save the images to the images array
  }

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:   (NSIndexPath *)indexPath
{

   static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil)
  {
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    // Set up the cell...
   }


      cell.imageView.image=[UIImage imageNamed:@"placeholder.png"]; //placeholder image is blank image displayed till the image loads

    if ([dicImages_msg valueForKey:[images objectAtIndex:indexPath.row]])
    {

        if (images.count == 0)
        {
            cell.imageView.image=[UIImage imageNamed:@"placeholder.png"];
        }

        else
        {
            cell.imageView.image=[dicImages_msg valueForKey:[images objectAtIndex:indexPath.row]];
        }
    }

    else
    {

        if (!isDragging_msg && !isDecliring_msg)
        {
            [self performSelectorInBackground:@selector(downloadImage_3:) withObject:indexPath];
        }

        else
        {
            cell.imageView.image=[UIImage imageNamed:@"placeholder.png"];
        }
    }


return cell;

}
-(void)downloadImage_3:(NSIndexPath *)path
{

    NSAutoreleasePool *pl = [[NSAutoreleasePool alloc] init];

    UIImage *img=[[UIImage alloc]init];
            img=[UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:[images objectAtIndex:path.row]]]];

    [dicImages_msg setObject:img forKey:[images objectAtIndex:path.row]];

     [self.YourTable performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];

     [pl release];

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14438442

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档