如何通过RSS源从网站获取图像?

时间:2014-04-13 01:53:35

标签: ios image uiimageview rss nsxmlparser

我目前正在构建一个iOS RSS提要应用程序,它将有一个显示提要的tableView。我希望在每个表格视图单元格中放置一个ImageView,图像将来自该RSS源的网站。我目前在我的文件中使用NSXMLParserDelegate。我只需要知道如何从网站上获取图像......这里只是我正在使用的rss feed的一个例子。 http://www.forbes.com/most-popular/feed/是否可以从此网址获取图片?谢谢你的帮助!到目前为止,这是我的代码。为了获得图像,我需要添加什么?

#import "JSSSecondViewController.h"
#import "JSSDetailViewController.h"
#import "JSSFirstViewController.h"

@interface JSSSecondViewController () {
    NSXMLParser *parser;
    NSMutableArray *feeds;
    NSMutableDictionary *item;
    NSMutableString *title;
    NSMutableString *link;
    NSString *element;
}

@end

@implementation JSSSecondViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    feeds = [[NSMutableArray alloc] init];
    NSURL *url = [NSURL URLWithString:@"http://rss.cnn.com/rss/cnn_tech.rss"];
    parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
    [parser setDelegate:self];
    [parser setShouldResolveExternalEntities:NO];
    [parser parse];

    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return feeds.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"title"];
    return cell;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {

    element = elementName;

    if ([element isEqualToString:@"item"]) {

        item    = [[NSMutableDictionary alloc] init];
        title   = [[NSMutableString alloc] init];
        link    = [[NSMutableString alloc] init];

    }

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

    if ([elementName isEqualToString:@"item"]) {

        [item setObject:title forKey:@"title"];
        [item setObject:link forKey:@"link"];

        [feeds addObject:[item copy]];

    }

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

    if ([element isEqualToString:@"title"]) {
        [title appendString:string];
    } else if ([element isEqualToString:@"link"]) {
        [link appendString:string];
    }

}

- (void)parserDidEndDocument:(NSXMLParser *)parser {

    [self.tableView2 reloadData];

}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"showDetail"]) {

        NSIndexPath *indexPath = [self.tableView2 indexPathForSelectedRow];
        NSString *string = [feeds[indexPath.row] objectForKey: @"link"];
        [[segue destinationViewController] setUrl:string];

    }
}



@end

这是我要解析的XML ......

<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
<channel>
<title>CNET Gaming</title>
<link>http://www.cnet.com/#ftag=CADa872701</link>
<description>
Game on! Get the latest in gaming news, video game reviews, computer games & video game consoles.
</description>
<language>en</language>
<pubDate>Thu, 10 Apr 2014 23:23:50 GMT</pubDate>
<lastBuildDate>Thu, 10 Apr 2014 23:23:50 GMT</lastBuildDate>
<ttl>2</ttl>
<image>
<title>CNET Gaming</title>
<url>
http://i.i.cbsi.com/cnwk.1d/i/ne/gr/prtnr/CNET_Logo_150.gif
</url>
<link>http://www.cnet.com/#ftag=CADa872701</link>
</image>
<item>
<title>
Get a Moga Mobile Gaming System for Android for $13 shipped
</title>
<link>
http://www.cnet.com/news/get-a-moga-mobile-gaming-system-for-android-for-13-shipped/#ftag=CADa872701
</link>
<description>
This Bluetooth game controller sold for $49.99 when it debuted last year.<img width='1' height='1' src='http://cnet.com.feedsportal.com/c/34938/f/645455/s/38e7da50/sc/5/mf.gif' border='0'/><br clear='all'/><br/><br/><a href="http://da.feedsportal.com/r/193360522992/u/191/f/645455/c/34938/s/38e7da50/sc/5/rc/1/rc.htm" rel="nofollow"><img src="http://da.feedsportal.com/r/193360522992/u/191/f/645455/c/34938/s/38e7da50/sc/5/rc/1/rc.img" border="0"/></a><br/><a href="http://da.feedsportal.com/r/193360522992/u/191/f/645455/c/34938/s/38e7da50/sc/5/rc/2/rc.htm" rel="nofollow"><img src="http://da.feedsportal.com/r/193360522992/u/191/f/645455/c/34938/s/38e7da50/sc/5/rc/2/rc.img" border="0"/></a><br/><a href="http://da.feedsportal.com/r/193360522992/u/191/f/645455/c/34938/s/38e7da50/sc/5/rc/3/rc.htm" rel="nofollow"><img src="http://da.feedsportal.com/r/193360522992/u/191/f/645455/c/34938/s/38e7da50/sc/5/rc/3/rc.img" border="0"/></a><br/><br/><a href="http://da.feedsportal.com/r/193360522992/u/191/f/645455/c/34938/s/38e7da50/sc/5/a2.htm"><img src="http://da.feedsportal.com/r/193360522992/u/191/f/645455/c/34938/s/38e7da50/sc/5/a2.img" border="0"/></a><img width="1" height="1" src="http://pi.feedsportal.com/r/193360522992/u/191/f/645455/c/34938/s/38e7da50/sc/5/a2t.img" border="0"/>
</description>
<pubDate>Thu, 10 Apr 2014 23:21:10 GMT</pubDate>
<guid>
http://www.cnet.com/news/get-a-moga-mobile-gaming-system-for-android-for-13-shipped/#ftag=CADa872701
</guid>
<dc:creator>Rick Broida</dc:creator>
<media:thumbnail url="http://cnet3.cbsistatic.com/hub/i/r/2014/04/02/0eeab98d-efb9-4779-a973-7b709f4fbb06/thumbnail/300x230/c7edbbac37a02b874a7bcfe373f6f9ec/moga-mobile.jpg"/>
</item>
<item>
<title>Xbox boss: Microsoft won't sell us off</title>
<link>
http://www.cnet.com/news/xbox-boss-microsoft-wont-sell-us-off/#ftag=CADa872701
</link>

这只是一个片段。它包含对图像的引用。在这种情况下,我如何在tableView中显示图像?

0 个答案:

没有答案