2011-10-07 59 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