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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…