iphone - Xcode Get image url from rss feed -


newbie xcode , thrilled answer question. trying image url tag rss feed , add custom table view cell. other text fine.

this rss row:

<item> <title>this title</title> <description>this 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 part of code without 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

first image url attributes dictionary in enclosure element inside 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 item array inside 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 rss feeds. recommend put nslog statements inside didstartelement understand image url can found:

nslog(@"\nxml parser:didstartelement:%@\n\t\t\tnamespaceuri:%@\n\t\t\tqualifiedname:%@\n\t\t\tattributes:%@", elementname, namespaceuri, qname, attributedict); 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -