Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
946 views
in Technique[技术] by (71.8m points)

ios - MWFeedParser - RSS with images

I have problem, I'm using MWFeedParser Rss reader in my iOS aplication and it works well, but I need to fetch the images from my feed. Can you help me please?

Here is url of that MWFeedParser project: GitHub

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I have used this in my cellForRowAtIndexPath function so that it searches for images as the cell is displayed

MWFeedItem *item = itemsToDisplay[indexPath.row];
if (item) {
    NSString *htmlContent = item.content;
    NSString *imgSrc;

    // find match for image
    NSRange rangeOfString = NSMakeRange(0, [htmlContent length]);
    NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"(<img.*?src=")(.*?)(".*?>)" options:0 error:nil];

    if ([htmlContent length] > 0) {
        NSTextCheckingResult *match = [regex firstMatchInString:htmlContent options:0 range:rangeOfString];

        if (match != NULL ) {
            NSString *imgUrl = [htmlContent substringWithRange:[match rangeAtIndex:2]];
            NSLog(@"url: %@", imgUrl);

            //NSLog(@"match %@", match);
            if ([[imgUrl lowercaseString] rangeOfString:@"feedburner"].location == NSNotFound) {
                imgSrc = imgUrl;
            }
        }
    }
}

Note I am also ignoring the image if it has 'feedburner' in the url to avoid feedburner type icons.

I am also using AFNetwork's class when I show the image later

    if (imgSrc != nil && [imgSrc length] != 0 ) {
        [myimage setImageWithURL:[NSURL URLWithString:imgSrc] placeholderImage:[UIImage imageNamed:IMAGETABLENEWS]];
    } else {
        NSLog(@"noimage");
        cell.imageView.image = [UIImage imageNamed:IMAGETABLENEWS];
        //[myimage setImage:[UIImage imageNamed:IMAGETABLENEWS]];
    }

I have left in my commented NSLog parts so you can uncomment and check if you want

Make sure you have an IMAGETABLENEWS constant for the placeholder or get rid of that part as you need.

This is only a very simple check of images in the html text and is not comprehensive. It served my purpose and may help you get your logic right for doing something more detailed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...