Xcode Get image url from rss feed

2020-06-26 05:20发布

Newbie to Xcode and would be thrilled to get an answer to this question. I am trying to get the image url tag from a rss feed and add it to my custom table view cell. The other text I get fine.

This is the rss row:

<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>

This is part of my code without any image logic:

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

}

Thanks

2条回答
Emotional °昔
2楼-- · 2020-06-26 06:12

Try this:

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

Hope this helps.

查看更多
姐就是有狂的资本
3楼-- · 2020-06-26 06:16

First get the image URL from the attributes dictionary in the enclosure element inside the didStartElement method:

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

}

Then add imageUrl to your item array inside the didEndElement method:

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

}

}

This may not work for all rss feeds. I recommend you put NSLog statements inside the didStartElement to understand where the image url can be found:

NSLog(@"\nXML Parser:didStartElement:%@\n\t\t\tnameSpaceURI:%@\n\t\t\tqualifiedName:%@\n\t\t\tattributes:%@", elementName, namespaceURI, qName, attributeDict);
查看更多
登录 后发表回答