我使用以下代码在表格视图中显示图像,
-(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中的图像。
我搜索了解决方案,但我无法得到解决方案,
提前致谢。
答案 0 :(得分:1)
你必须为他的图片网址提出网址请求。而不是UILabel,放置一个UIImageView,并将来自url响应的图像数据加载到imageview中。
答案 1 :(得分: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
这样的方法来查看缓存中的图像,并在需要时异步加载它,将它存储到缓存中并显示它。
答案 2 :(得分:0)
获得所有图片网址后。尝试以下代码来显示它! : - )
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;
答案 3 :(得分: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];
答案 4 :(得分:0)
使用AFNetworking framework为Teodor的答案添加更多内容将有助于您的生活。它适用于基于块的方法。更有帮助的是,它有AFImageRequestOperation,一个用于下载和处理图像的特定类,以及UIImageView + AFNetworking,它可以帮助您更加异步地加载远程图像。