CTSchmidt
CTSchmidt

Reputation: 1205

How do i get a image from RSS to cell

I'm working hard on a solution to my problem and I hope that someone can help me here. I have a RSS feed with formated Data. e.g.

<description>
    <![CDATA[
     <p> ... <a href="https://www.google.com/">Klick here</a>.</p>
    ]]>
</description>
<content:encoded>
    <![CDATA[<p> ... <a href="https://www.google.com/">Klick here</a>.</p> <img src="http://image_url.jpg" alt="" title="some text" width="622" height="466" class="size-full wp-image-117" />
    ]]>
</content:encoded>

Is there a possibility to apply the formatting to a label? or in better case to a WebView? what can I do to make the whole RSS feed is displayed?

For a better understanding in my code: Here is the source that I implement in my Project.

Upvotes: 2

Views: 737

Answers (1)

Rob
Rob

Reputation: 437882

In RSS readers, there are often two levels in one's user interface where you must consider how you'll handle this issue. The master view and the details view. We'll tackle those one at a time:

Master view

In the master view where you're looking at the list of items in the feed. In this case, most RSS readers will present just the plain text title of the item and perhaps the image associated with that item. The question here is where to retrieve the image URL. There are (as far as I know) three options here:

  1. The url attribute of the media:content element. See http://news.yahoo.com/rss/ for an example.

  2. The url element inside the image element. See http://www.dpreview.com/feeds/news.xml for an example.

  3. Parsing the HTML img tag from within the description or content:encoded elements. You can either run HTML through another parser (though, sadly, NSXMLParser is not very robust at parsing HTML, so you might have to pursue something like Hpple) or just use regular expressions to get the image URLs out of the HTML:

    - (NSArray *)imgSrcAttributesInString:(NSString *)string
    {
        NSError *error = NULL;
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<img\\s[\\s\\S]*?src\\s*?=\\s*?['\"](.*?)['\"][\\s\\S]*?>"
                                                                               options:NSRegularExpressionCaseInsensitive
                                                                                 error:&error];
    
        NSMutableArray *results = [NSMutableArray array];
    
        [regex enumerateMatchesInString:string
                                options:0
                                  range:NSMakeRange(0, [string length])
                             usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
    
                                 [results addObject:[string substringWithRange:[result rangeAtIndex:1]]];
                             }];
    
        if ([results count] > 0)
            return results;
    
        return nil;
    }
    

Which of these techniques you employ, depends upon your RSS source and the amount of work you're willing to go through. I think most RSS readers just go through 1 and/or 2, but not 3.

Details view

The second is the details view, where you're looking at the full details associated with an RSS item. One very simple approach is to not use a tableview for the details view, but rather just have a standard UIViewController with a UIWebView and display the results that way.

In answer to your question about labels, prior to iOS 6.0 you could not apply formatting to a UILabel. Effective in iOS 6.0, you can use the attributedText property. But that does not support HTML. There are attempts to bridge that gap (see https://stackoverflow.com/a/4652521/1271826), but it seems like a lot more work than just using a UIWebView.

Upvotes: 3

Related Questions