For most cases, a RSS feed is nothing but an XML document providing title, link description etc. of the latest content pages of a website. So when building RSS Feed display widget or a view where we want to display an image about the post next to the title along with some teaser text? The image is normally part of the description of the RSS item. In such cases the below snippet helps to find search for the 1st image that if present in the html and returns the link(URL). It basically uses NSScanner to scan to find the beginning of the image tag like <img src=\ tag till it find the closing tag /, then it extracts the inner text which is nothing but the image URL. Once we get the URL we can Asynchronously load into a image view.
<!--break-->
Code Snippet
The following code snippet shows the main methods.
+ (NSString *)getFirstImageUrl: (NSString *) html {
NSScanner *theScanner;
NSString *text = nil;
theScanner = [NSScanner scannerWithString: html];
// find start of tag
[theScanner scanUpToString: @"<img src=\"" intoString: NULL];
if ([theScanner isAtEnd] == NO) {
NSInteger newLoc = [theScanner scanLocation] + 10;
[theScanner setScanLocation: newLoc];
// find end of tag
[theScanner scanUpToString: @"\"" intoString: &text];
}
return text;
}