我有问题,我在我的iOS应用程序中使用MWFeedParser Rss阅读器,它工作得很好,但我需要从我的提要中获取图像。你能帮帮我吗?
发布于 2013-07-14 11:44:56
我在我的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;
}
}
}
}请注意,如果图像在url中有' 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文本中的图像的非常简单的检查,并且不全面。它服务于我的目的,并可能帮助您正确的逻辑来做一些更详细的事情。
发布于 2016-01-23 18:19:28
如果您的MWFeedItem在其enclosure-tag中嵌入了图像,您可能需要考虑执行以下操作:
MWFeedItem有一个名为enclosures的属性。它是一个包含一个或多个字典的数组。
本词典是在- (BOOL)createEnclosureFromAttributes:(NSDictionary *)attributes andAddToItem:(MWFeedItem *)currentItem (MWFeedParser.M)中生成的。
这些字典有三个关键字(如果可用):url、type和length。
第一个可能就是你要找的那个。我设法做到了这一点:
提要示例
<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
}https://stackoverflow.com/questions/15254500
复制相似问题