2014-01-29 323 views
0

我需要幫助從RSS源獲取URL。我試圖從描述中獲取圖片網址,但是當我嘗試使用它時它爲空。從RSS獲取URL描述

這裏是飼料:

<item> 
<title>Photo</title> 
<description> 
<img src="http://24.media.tumblr.com/c0d9f18c016c3bd45a84edc58d5c9d1b/tumblr_mv6nhvj0Vs1qjbzjlo1_500.jpg"/><br/><br/> 
</description> 
<link> 
http://littleheartrecords.tumblr.com/post/64965673651 
</link> 
<guid> 
http://littleheartrecords.tumblr.com/post/64965673651 
</guid> 
<pubDate>Thu, 24 Oct 2013 13:05:55 -0400</pubDate> 
</item> 

這裏就是我得到它:

else if ([elementName isEqual:@"description"]) 
    { 
     currentString = [[NSMutableString alloc]init]; 
     [self setInfoString:[attributeDict valueForKey:@"img src"]]; 
    } 

這裏是我嘗試使用它:

NSLog(@"infoString: %@", [item infoString]); 
    NSURL *imageLink = [NSURL URLWithString:[item infoString]]; 
    NSData *data = [NSData dataWithContentsOfURL:imageLink]; 
    UIImage *image = [[UIImage alloc]initWithData:data]; 

回答

0

試試這個

in didStartElement:

else if ([elementName isEqual:@"description"]) { 
    ... 
    isDescription = YES; 
} else if ([elementName isEqual:@"img"]) { 
    if (isDescription) { 
     NSString *urlString = [attributeDict valueForKey:@"src"]; 
     NSLog(@"urlString : %@", urlString); 
     [self setInfoString:urlString]; 
     //or 
     [item setInfoString:urlString]; 
    } 
} 

didEndElement:

else if ([elementName isEqual:@"description"]) { 
    ... 
    isDescription = NO; 
} 
+0

仍然不起作用。 – raginggoat

+0

什麼是'setInfoString:'方法。嘗試使用'[item setInfoString:[attributeDict valueForKey:@「src」]];' – Akhilrajtr

+0

它仍然不給我任何東西。 – raginggoat

0

使用XMLReader類作爲它非常容易使用。

Example

NSData *data = ...; // some data that can be received from rss 
NSError *error = nil; 
NSDictionary *dict = [XMLReader dictionaryForXMLData:data 
             options:XMLReaderOptionsProcessNamespaces 
              error:&error]; 

注意使用NSDictionaryclasses返回得到url string