retrive image from feed parser

Discussion in 'iOS Programming' started by preiostd, Jun 22, 2013.

  1. preiostd macrumors newbie

    Oct 27, 2012
    I am using MWFeedParser to download feeds in my app. So far it's working fine - however, I donnot know how to retrive image from feed and view in the detailed article??
  2. truehybridx macrumors member

    Dec 6, 2010

    Right now MWFeedParser only lets you get particular things from the feed

    item.title (NSString) (NSString) (NSDate)
    item.updated (NSDate)
    item.summary (NSString)
    item.content (NSString)
    item.enclosures (NSArray of NSDictionary with keys url, type and length)
    item.identifier (NSString)

    In most cases the images are in the form of links inside item.content.
    So you will have to parse out the links on your own and download the images for them.

    I started using that parser but since it didnt do images already I ended up making my own using AFNetworking, SDWebImage and using a JSON feed on Wordpress (but that requires your feed to be JSON instead of RSS which the format can be different depending on the site/feed)
  3. preiostd thread starter macrumors newbie

    Oct 27, 2012
    Thanks 4 r replay :)
    Itried to parse each article for it's image URL. This is the code I have currently:
    // MWFeedItem.h
    NSData* firstImg;
    @property (nonatomic, retain) NSData* firstImg;
    add synthesize and dealoc

    // RootViewController.m
    - (NSString *)getFirstImage:(NSString *)htmlString{
    NSScanner *theScanner;
    NSString *text = nil;
    theScanner = [NSScanner scannerWithString: htmlString];
    // find start of tag
    [theScanner scanUpToString: @"<img src=\"" intoString: NULL];
    if ([theScanner isAtEnd] == NO) {
        NSInteger newLoc = [theScanner scanLocation] + 10;
        [theScanner setScanLocation: newLoc];
        // find end of tag
        [theScanner scanUpToString: @"\"" intoString: &text];
    return text;
    (void)feedParser:(MWFeedParser *)parser didParseFeedItem:(MWFeedItem *)item {
    NSLog(@"Parsed Feed Item: “%@”", item.title);    
    NSString* str_imageUrl = [self getFirstImage:item.summary];    
    item.firstImg = [NSData dataWithContentsOfURL:[NSURL URLWithString:str_imageUrl]];     
    if (item) [parsedItems addObject:item];      

    but still I can't view the image:(
  4. truehybridx macrumors member

    Dec 6, 2010
    Does your function getFirstImage: even return a link to an image?

    I would also put that if (item) line at the top of its function so you arent trying to get a link from a bad item.

    Theoretically it should work, though later i would change the loading image to something more background
  5. preiostd thread starter macrumors newbie

    Oct 27, 2012
  6. truehybridx macrumors member

    Dec 6, 2010
    Then you definately need to get that function working :D

Share This Page