【问题标题】:How to display image from a RSS feed如何显示来自 RSS 提要的图像
【发布时间】:2012-11-07 13:50:04
【问题描述】:

我正在尝试构建一个显示来自http://news.yahoo.com/rss/ 的最新 RSS 提要的应用程序 我可以使用 NSXMLParserDelegate 解析 xml,我可以显示日期和时间以及特定提要的标题,但我也想为每个提要显示一个图像,我知道如何解析主要元素但我不知道如何传递以 url 作为键的子元素的属性...例如上述提要包含以下内容:

<item>
  <title>fooo</title>
  <description ahref="some link" image src="http://l1.yimg.com/bt/api/res/1.2/Wo3_apH.kz7DMvOj7MDtRQ--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9ODU7dz0xMzA-/http://media.zenfs.com/en_us/News/ap_webfeeds/20c464ff99815420210f6a706700a792.jpg"
     </description>

我需要表格视图中来自 IMG SRC 的图像以及该特定提要的标题和日期。我很困惑如何解析 IMG SRC 属性并将其添加到我的 tableview 并改变大小,这样它就不会破坏我的标题和日期。

到目前为止我做了什么:

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];
    }
}

找到的字符

- (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;
}

如何获取单元格右侧的图像?

【问题讨论】:

  • ` fooo l1.yimg.com/bt/api/res/1.2/Wo3_apH.kz7DMvOj7MDtRQ--/…" `
  • 在这里你必须解析链接和元数据......从链接下载图像,据我所知,最有效的方法是创建一个对象类来存储你必须的值显示在 tableview 单元格上,将其存储在一个可变数组中,其中包括您将要下载的图像。
  • 该示例 Feed 不正确。您缺少“>”,image 部分看起来完全错误。请确认。

标签: objective-c ios nsdictionary nsxmlparser


【解决方案1】:

在回答标题中的问题时,要加载图像,您使用 NSXMLParser 对象解析字符串 URL 的 RSS 提要,当您想将该图像呈现给用户时,您使用该 URL要将图像检索到NSData,为此创建一个UIImage,然后相应地设置UIImageViewimage 属性。请参阅下面列表中的cellForRowAtIndexPath

就实现的具体细节而言,我已根据您正在阅读来自 http://news.yahoo.com/rss/ 的提要这一事实对这个答案进行了重大修改,该提要的格式与您在示例 XML 中显示的格式大不相同清单。格式实际上似乎是:

<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://the.url.for.image/images/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 等,您可以在其中获取打开和关闭标签之间的值(您可以使用NSXMLParserDelegate 方法parser:foundCharacters:)。但是图片的URL其实是在media:content标签本身的url属性里面,在这种情况下你必须使用NSXMLParserDelegate方法didStartElement查询attributes参数。

因此,鉴于此,我上传了一个sample Yahoo News reader,它演示了解析雅虎 RSS 提要的过程。请注意,这是一个相当原始的实现,但希望这可以为您提供基本概念。

【讨论】:

  • 我了解您在上面提供给我的代码,但我对如何检索子元素属性以及在检索到数组中的所有 url 后应该在哪里使用上述代码感到非常困惑?除了 NSXMLParser 你能推荐我任何其他简单的解析器吗?谢谢
  • 上面的代码真的很酷谢谢..但我不明白最后一行 self.imageView.image = image;我想显示每个提要的图像
  • 我做了以下pastie.org/5402327 但我遇到了 NSArray 和 NSString 冲突,此外我的 NSLog 没有显示我的数组中的任何内容:-(
猜你喜欢
  • 1970-01-01
  • 2019-03-06
  • 1970-01-01
  • 2015-04-11
  • 1970-01-01
  • 2020-08-25
  • 1970-01-01
  • 2012-06-01
  • 2013-03-07
相关资源
最近更新 更多