我很难将我的RSS Feed中的图像提取到Xcode。我已经能够获得日期,标题,甚至类别。然而,图像位于描述的第一行,如下面的RSS提要所示:
<item>
<title>Stevie Wonder Tribute</title>
<link>http://celebirious.com/bey/?p=6925</link>
<comments>http://celebirious.com/bey/?p=6925#comments</comments>
<pubDate>February 17, 2015</pubDate>
<dc:creator>
<![CDATA[ Haley ]]>
</dc:creator>
<category>
<![CDATA[ Uncategorized ]]>
</category>
<guid isPermaLink="false">http://celebirious.com/bey/?p=6925</guid>
<description>
<![CDATA[
<img src="http://i2.wp.com/celebirious.com/bey/wp-content/uploads/2015/02/normal_Tribute-004.jpg?fit=1024%2C1024" class="attachment-large wp-post-image" alt="normal_Tribute-004" />Beyonce performed during the Stevie Wonder: Songs In The Key Of Life – An All-Star GRAMMY Salute held at Nokia Theatre L.A. Live on February 10, 2015. Watch her performance below!
]]>
</description>
[..]
</item>
XCode看起来像这样。
@interface APPMasterViewController () {
NSXMLParser *parser;
NSMutableArray *feeds;
NSMutableDictionary *item;
NSMutableString *title;
NSMutableString *pubDate;
NSMutableString *category;
NSMutableString *link;
NSMutableString *description;
NSString *element;
}
然后我使用SDWEBImage尝试外包图像:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
carecell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.thetitle.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"title"];
NSString * imageURL = [[feeds objectAtIndex:indexPath.row] objectForKey: @"description"];
[cell.theimage sd_setImageWithURL:[NSURL URLWithString:imageURL]
placeholderImage:[UIImage imageNamed:@"candid-02.jpg"]];
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];
pubDate = [[NSMutableString alloc] init];
category = [[NSMutableString alloc] init];
description = [[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"];
[item setObject:pubDate forKey:@"pubDate"];
[item setObject:category forKey:@"category"];
[item setObject:description forKey:@"description"];
[feeds addObject:[item copy]];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if ([element isEqualToString:@"title"]) {
[title appendString:string];
}
if ([element isEqualToString:@"pubDate"]) {
[pubDate appendString:string];
}
if ([element isEqualToString:@"category"]) {
[category appendString:string];
}
if ([element isEqualToString:@"description"]) {
[description appendString:string];
}
else if ([element isEqualToString:@"link"]) {
[link appendString:string];
}
}
我觉得有些东西不见了,有人可以帮忙吗?