MWFeedParser - 带图像的RSS [英] MWFeedParser - RSS with images

查看:89
本文介绍了MWFeedParser - 带图像的RSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题,我在我的iOS应用程序中使用MWFeedParser Rss阅读器并且效果很好,但我需要从我的Feed中获取图像。你能帮我吗?

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?

这是MWFeedParser项目的网址:GitHub

推荐答案

我在我的cellForRowAtIndexPath函数中使用了它,以便在显示单元格时搜索图像

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;
            }
        }
    }
}

注意如果它在网址中有'feedburner'以避免使用feedburner类型图标,我也会忽略该图像。

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

我稍后在显示图像时也使用AFNetwork的类

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]];
    }

我已经留下了我评论的NSLog部分,所以你可以取消注释并检查你是否想要

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

确保占位符有一个IMAGETABLENEWS常量,或者根据需要摆脱该部分。

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

这只是对html文本中图像的一个非常简单的检查,并不全面。它符合我的目的,可以帮助你把你的逻辑做得更加详细。

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.

这篇关于MWFeedParser - 带图像的RSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆