Christophorus
Christophorus

Reputation: 154

DIsplaying a tumbnail image from an RSS feed into tableView cell

I am creating an RSS feed app for work and I pretty much have most done except for one thing, getting the image for each article to load into the thumbnail UIImage View. I've searched around the web for some answers but still havent figured it out after a few days. Here is the code I have so far related to images :

In my MWFeedParser.m class :

else if ([currentPath isEqualToString:@"/rss/channel/item/media:thumbnail"])
{
    if ([currentElementAttributes objectForKey:@"url"])
        {
            item.thumbnailURL = [currentElementAttributes valueForKey:@"url"];
                            processed = YES;
        } 
}

In my RSSMaster.m class :

 NSString *imageURLstr = item.thumbnailURL ? item.thumbnailURL : @""; 

In my RSSResults.m class:

NSLog(@"IMAGE URL: %@", imageURLstr);

if (![imageURLstr isEqualToString:@""]) { 
NSURL *imageURL = [NSURL URLWithString:imageURLstr];

//image download
dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(q, ^{
/* Fetch the image from the server... */
NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *img = [[UIImage alloc] initWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
/* This is the main thread again, where we set the tableView's image to
be what we just fetched. */
cell.thumbnail.image = img;
        });
    });
} 

The image starts in the code at src= and is follwed by the artcile description. So my question is, how do I code my project so that the individual image link for each article gets pulled into my thumnail UIImage view in my tableView cells?

Any kind of detailed help would be greatly appreciated so that I can finally be done with this, thank you !

Upvotes: 0

Views: 425

Answers (1)

Alaeddine
Alaeddine

Reputation: 6212

You can use an XML Parser, or you can treat it as NSString and extract the src attribute like this :

NSString *imageURLstr=item.summary;
imgURL = [imgURL substringToIndex:[imgURL rangeOfString:@"alt="].location-2];
imgURL = [imgURL substringFromIndex:[imgURL rangeOfString:@"src="].location+[@"src=" length]+1];

NSLog(@"src: %@",imgURL);

Upvotes: 1

Related Questions