2013-01-21 92 views
2

加载图像我解析有链接的图片XML文件,我想在一个UITableView显示它们。出于某种原因,他们没有出现在视图中。我认为这个问题与我的解析脚本没有任何关系,因为它正在显示表格中的文本。这是我的代码,用于显示图片:XML解析 - 从URL中

- (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就得到了图像的URL?

THX对于任何帮助 安东尼

+0

难道您将图像保存在文档目录中ectory或直接从url中获取.....? –

+0

使用此项目作为参考,它会使你的学习解决问题的方法:http://developer.apple.com/library/ios/samplecode/LazyTableImages/LazyTableImages.zip – viral

+0

嗨得到它的工作! thx寻求帮助。我的一些[currentTweet图片]链接在URL前面缺少“http://”。我如何添加? –

回答

2
- (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; 
} 
+0

嗨,thx!它运作良好。我现在得到的问题是我需要在我的[currentTweet pic]前添加http://。我不能简单地使用@“”......任何建议? –

+0

如果你想修改字符串,你可以使用NSMutableString。或者创建一个新的[NSString stringWithFormat:@“http://%@”,oldString] – pbibergal

1
@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]; 

}