2011-10-07 75 views
0

我已经创建了一个简单的XML解析器来解析我的rss源到我的应用程序。 id,currentstatus,从xml文件中解析简单的图片。 但我无法从XML文件中获取图像。 。我从xml文件中检索所有图像。但noimage.jpg不显示。你可以请任何帮助我。如何在IOS中打印xml图像?

XML代码

<Agent> 
<id>3422</id> 
<currentstatus>Logged Off</currentstatus> 
<picture>1</picture> 
</Agent> 
<Agent> 
<id>3432</id> 
<currentstatus>Logged Off</currentstatus> 
<picture>0</picture> 
</Agent> 

我尝试下面的代码:

UIImage * cellimages = [[UIImage alloc]init]; 
NSString *string1 = @"http://telecomstats.co.uk/images/LLReaderProfile/"; 
NSString *cellvalue =[[[[self rssParser]rssItems]objectAtIndex:indexPath.row]picture]; 
NSString* disable = [[NSString alloc] initWithString:@"0"]; 


if (cellvalue == disable) 
{ 
cellimages = [UIImage imageWithContentsOfFile:@"http://telecomstats.co.uk/images/LLReaderProfile/noimage.jpg"]; 
} 
else  { 

    cellimages = [string1 stringByAppendingString:[[[[self rssParser]rssItems]objectAtIndex:indexPath.row]id]]; 
    cellimages = [cellimages stringByAppendingString: @".jpg"]; 

} 

cellimages = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:cellimages]]]; 
cell.imageView.image = cellimages; 

是否有人能帮助我吗? 感谢您的帮助和见解。

+0

URL是否正确?至少noimage网址提供了一个未找到的域名。 –

+0

感谢您的重播抱歉,我输入了错误的URL正确的URL是:static.telecomstats.co.uk/images/psychic/LLReaderProfile/...我尝试了以下方法以及cellimages = [string1 stringByAppendingString:@“noimage.jpg” ]。但没有用 – Livelines

回答

0

您正在使用cellimages这是一个UIImage对象来将字符串存储在您的else分支中。此外,当cellvalue == disable是真实的,在执行此:

cellimages = [UIImage imageWithContentsOfFile:@"http://telecomstats.co.uk/images/LLReaderProfile/noimage.jpg"]; 
cellimages = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:cellimages]]]; 

有你在[NSURL URLWithString:]把一个UIImage这将可能无法正常工作;实际上这个代码应该会产生相当多的警告。尝试正确分隔URL字符串和要显示的图像对象。在确定了正确的URL后,将所有URL填入NSString,然后获得UIImage

请注意,您的代码还有其他一些问题,特别是您使用的是dataWithContentsOfURLimageWirhContentsOfFile,它们都会阻塞,直到图像被检索到。这会导致应用程序在使用不良网络连接时挂起,如果网络速度太慢,会导致应用程序崩溃。查看异步下载并使用AFNetworking等框架。

+0

谢谢你的好意。我会尝试。在我看来,如果条件是错误的,因为所有的读者照片都显示,但只有noimages.jpg不显示。 – Livelines

+0

这是因为当条件为真(if-branch)时,将'cellimages'设置为'UIImage'对象。如果采用else分支,'cellimages'包含一个由'NSURL'正确处理的字符串,这就是为什么所有其他图像都能正确显示的原因。如果你想在'if-branch'中设置'cellimages'为一个字符串(例如完整的URL),它可能会起作用,但是对于这两个URL字符串(类型为'NSString')和图像类型'UIImage')是非常糟糕的做法。 –

+0

我试过像if([[[[[self rssParser] rssItems] objectAtIndex:indexPath.row] picture] ==禁用) 但没用。有什么办法可以解决这个问题。如果你知道,请给我提示。因为我是新的这个领域。基本上我是一个网页设计师。现在我完成了除此之外的所有功能。 谢谢 – Livelines