MWFeedParser - 带图像的RSS

时间:2013-03-06 17:53:40

标签: ios xcode image rss

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

Here is url of that MWFeedParser project: GitHub

2 个答案:

答案 0 :(得分:3)

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

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类型图标。

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

    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部分,因此您可以取消注释并检查是否需要

确保占位符具有IMAGETABLENEWS常量,或根据需要删除该部分。

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

答案 1 :(得分:1)

如果您的MWFeedItem内嵌了图片enclosure-tag,您可能需要考虑执行以下操作:

MWFeedItem有一个名为enclosures的属性。它是一个包含一个或多个词典的数组。

这个字典是在中生成的 - (BOOL)createEnclosureFromAttributes:(NSDictionary *)attributes andAddToItem:(MWFeedItem *)currentItemMWFeedParser.M)。

这些词典有三个键(如果可用):urltype&amp; length

第一个可能是你正在寻找的那个。我设法得到这样:

Feed示例

    <item>
        <title>Item title</title>
        <link>http://www.yourdomain.com</link>
        <description>Item description</description>
        <pubDate>Mon, 01 Jan 2016 12:00:00 +0000</pubDate>
        <enclosure url="http://www.yourdomain.com/image.jpg" length="0" type="image/jpeg"></enclosure>
        <category>Algemeen</category>
    </item>

请注意<enclosure></enclosure>

中的图片链接

<强> YourViewController.m

- (void)feedParser:(MWFeedParser *)parser didParseFeedItem:(MWFeedItem *)item {
    NSArray *EnclosureArray = item.enclosures;
    NSDictionary *ImageDict = [EnclosureArray objectAtIndex:0]; // 0 Should be replaced with the index of your image dictionary.
    NSString *ImageLink = [ImageDict objectForKey:@"url"];

    // Returns: http://www.yourdomain.com/image.jpg
}
相关问题