Xcode从rss feed获取图片网址

时间:2013-08-13 10:43:38

标签: iphone xcode rss nsxmlparser

新手到Xcode,很高兴得到这个问题的答案。我试图从rss feed获取图像url标记并将其添加到我的自定义表格视图单元格。另一篇文章我没事。

这是rss行:

<Item>
<title>this is a title</title>
<description>this is a description</description>
<link>http://www.something.com</link>
<pubDate>Fri, 09 Aug 2013</pubDate>
<enclosure type="image/jpg" url="http://www.ThisIsTheUrlIWant.com" />
</item>

这是我的代码的一部分,没有任何图像逻辑:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath {
CustomCellNews *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"  forIndexPath:indexPath];
cell.titleLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"title"];
cell.descriptionLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"description"];
//set cell image here
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];
    description = [[NSMutableString alloc] init];
    //image stuff
}
}

- (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:description forKey:@"description"];
    //image stuff

    [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];
} else if ([element isEqualToString:@"description"]) {
    [ingress appendString:string];
}
//image stuff

}

由于

2 个答案:

答案 0 :(得分:2)

首先从didStartElement方法中的enclosure元素中的属性字典中获取图像URL:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
//NSLog(@"Main View: didStartElement: %@\tstart", elementName);

element = elementName;

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

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

}


if ([element isEqualToString:@"enclosure"]) {
    imageType   =   [attributeDict objectForKey:@"type"];
    imageUrl    =   [attributeDict objectForKey:@"url"];

}
//NSLog(@"RSS Utility: didStartElement: %@", elementName);

}

然后将imageUrl添加到item方法中的didEndElement数组中:

- (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:description forKey:@"description"];
    [item setObject:imageType forKey:@"imageType"];
    [item setObject:imageUrl forKey:@"imageUrl"];
    [_feeds addObject:[item copy]];

}

}

这可能不适用于所有RSS Feed。我建议您将NSLog语句放在didStartElement中,以了解可以找到图片网址的位置:

NSLog(@"\nXML Parser:didStartElement:%@\n\t\t\tnameSpaceURI:%@\n\t\t\tqualifiedName:%@\n\t\t\tattributes:%@", elementName, namespaceURI, qName, attributeDict);

答案 1 :(得分:1)

试试这个:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if ([elementName isEqualToString:@"enclosure"]) {
        NSString *units = [attributeDict objectForKey:@"url"];
    }
}

希望这有帮助。

相关问题