0

我正在使用以下代码在表格视图中显示图像,

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[xmlParser templates] count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    Temp *currentTemplate = [[xmlParser templates] objectAtIndex:indexPath.row];

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

        CGRect contentNameLabelFrame = CGRectMake(64, 2, 250, 20);
        UILabel *contentNameLabelF = [[UILabel alloc] initWithFrame:contentNameLabelFrame];
        contentNameLabelF.tag = 0011;
        contentNameLabelF.font = [UIFont boldSystemFontOfSize:12];
        contentNameLabelF.text = @"Template Name:";
        contentNameLabelF.textColor = [UIColor orangeColor];
        [cell.contentView addSubview:contentNameLabelF];

        CGRect contentFrame = CGRectMake(115, 2, 250, 20);
        UILabel *contentLabel = [[UILabel alloc] initWithFrame:contentFrame];
        contentLabel.tag = 0012;
        contentLabel.font = [UIFont boldSystemFontOfSize:12];
        [cell.contentView addSubview:contentLabel];



        CGRect contentnumberofcontacts = CGRectMake(63, 20, 250, 13);
        UILabel *contentnumberofcontactsLabel = [[UILabel alloc] initWithFrame:contentnumberofcontacts];
        contentnumberofcontactsLabel.tag = 0013;
        contentnumberofcontactsLabel.font = [UIFont boldSystemFontOfSize:10];
        contentnumberofcontactsLabel.text = @"Image:";
        contentnumberofcontactsLabel.textColor = [UIColor brownColor];
        [cell.contentView addSubview:contentnumberofcontactsLabel];

        CGRect contentnumberFrame = CGRectMake(115, 20, 250, 13);
        UILabel *contentnumberLabel = [[UILabel alloc] initWithFrame:contentnumberFrame];
        contentnumberLabel.tag = 0014;
        contentnumberLabel.font = [UIFont systemFontOfSize:10];
        [cell.contentView addSubview:contentnumberLabel];


        CGRect contentstatus = CGRectMake(78, 35, 250, 10);
        UILabel *contentstatuslabel = [[UILabel alloc] initWithFrame:contentstatus];
        contentstatuslabel.tag = 0015;
        contentstatuslabel.font = [UIFont boldSystemFontOfSize:10];
        contentstatuslabel.text = @"Category:";
        contentstatuslabel.textColor = [UIColor grayColor];
        [cell.contentView addSubview:contentstatuslabel];

        CGRect statusFrame = CGRectMake(115, 35, 250, 10);
        UILabel *statusLabel = [[UILabel alloc] initWithFrame:statusFrame];
        statusLabel.tag = 0016;
        statusLabel.font = [UIFont systemFontOfSize:10];
        [cell.contentView addSubview:statusLabel];



    }

    UILabel *contentLabel = (UILabel *)[cell.contentView viewWithTag:0012];
    contentLabel.text = [currentTemplate templateName];

    UILabel *contentnumberLabel = (UILabel *)[cell.contentView viewWithTag:0014];
    contentnumberLabel.text = [currentTemplate imagePath];

    UILabel *statusLabel = (UILabel *)[cell.contentView viewWithTag:0016];
    statusLabel.text = [currentTemplate category];



    return  cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{
    return 75;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    xmlParser = [[ViewController alloc] loadXMLByURL:@"http://www.xxxxx.net/xxxxx/xxxx.aspx?type=xxxxx&xxxx="];
}

-(id)loadXMLByURL:(NSString *)urlString
{
    _templates = [[NSMutableArray alloc] init];
    NSURL *url = [NSURL URLWithString:urlString];
    NSData *data = [[NSData alloc] initWithContentsOfURL:url];
    parser = [[NSXMLParser alloc] initWithData:data];
    parser.delegate = self;
    [parser parse];

    return self;
}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if([elementName isEqualToString:@"template_maste"])
    {
        currentTemplate = [Temp alloc];
    }
    if ([elementName isEqualToString:@"img_path"])
    {
        currentTemplate = [Temp alloc];
        isStatus = YES;
    }
}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([elementName isEqualToString:@"Template_name"])
    {
        currentTemplate.templateName = currentNodeContent;
    }
    if([elementName isEqualToString:@"img_path"])
    {
        currentTemplate.imagePath = currentNodeContent;
    }
    if([elementName isEqualToString:@"Category"])
    {
        currentTemplate.category = currentNodeContent;
    }
    if([elementName isEqualToString:@"template_maste"])
    {
        [self.templates addObject:currentTemplate];
        currentTemplate = nil;
        currentNodeContent = nil;
    }
}

我能够在 tableView 中显示图像的路径,但我需要从 url 显示 tableView 中的图像。

我搜索了解决方案,但我无法获得解决方案,

提前致谢。

4

5 回答 5

1

如果您正在使用ASIHTTPRequest,您可以这样做:

NSURL *url = [NSURL URLWithString:@"http://example.com/picture.png"];
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setCompletionBlock:^{
    NSData *responseData = [request responseData];
    UIImage *img = [UIImage imageWithData:responseData];
    [self.myImageView setImage:img];
}];
[request startAsynchronous];

编辑:我还建议您将下载的图像(名称如 URL_MD5.png)保存在设备上,仅当它们在缓存中丢失时才从 Internet 获取它们。您也可以创建一个基于 UIImageView 的“组件”,它显示活动指示器(如 ajax),直到下载完成。因此,您可以添加一个类似的方法,该方法loadImageFromUrl将在缓存中查看图像并在需要时异步加载它,将其存储到缓存并最终显示。

于 2012-07-18T11:18:08.453 回答
1

您必须对所有图像的 URL 进行 url 请求。代替 UILabel,放置一个 UIImageView,并将图像数据从 url 响应加载到 imageview 中。

于 2012-07-18T11:10:33.783 回答
0

获得所有图像 URL 后。试试下面的代码来显示吧!!:-)

NSString *imageStr = [imageURLArray objectAtIndex:indexPath.row];
NSString *trimmedString = [imageStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *string1=[trimmedString stringByReplacingOccurrencesOfString:@"/n" withString:@""];
NSURL *url = [NSURL URLWithString:string1];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *newImage = [UIImage imageWithData:data];
cell.imageView.image = newImage;
于 2012-07-18T11:14:39.357 回答
0

我认为你正在寻找的是这样的:

NSString *path = @"http://www.yoururl/images/yourimage.png";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[[UIImage alloc] initWithData:data] autorelease];

UIImageView *imageView = [[[UIImageView alloc] initWithImage:img] autorelease];
imageView.frame = CGRectMake(0, 0, 44, 44);
[cell addSubview:imageView];
于 2012-07-18T11:15:04.780 回答
0

为 Teodor 的答案添加更多内容,使用AFNetworking 框架将有助于您的生活。它适用于基于块的方法。更有帮助的是,它有 AFImageRequestOperation,一个用于下载和处理图像的特定类,以及 UIImageView+AFNetworking,它可以帮助您更多地异步加载远程图像。

于 2012-07-18T11:15:08.767 回答