UIImage: How to get website tab icon
Asked Answered
A

3

6

I'm developing an RSS Reader and I need to get the favicon for each feed. For example, if my feed is google.com, I'd like to get the "G" icon and put it into a UIImage or something. Any ideas on how to achieve this?

Aristate answered 2/2, 2012 at 23:27 Comment(0)
S
21

The easiest way to go would be to use Google:

NSString *myURLString = @"http://www.google.com/s2/favicons?domain=www.stackoverflow.com";
NSURL *myURL=[NSURL URLWithString: myURLString];
NSData *myData=[NSData dataWithContentsOfURL:myURL];

UIImage *myImage=[[UIImage alloc] initWithData:myData];

That should work.

You would just have to replace the domain where you want to query your icon.

Samurai answered 2/2, 2012 at 23:37 Comment(3)
This used to work! Anyway, I tried it today and it returned an error just like the following: Error Domain=NSCocoaErrorDomain Code=256 "The file “favicon” couldn’t be opened." UserInfo=0x10a1e4150 {NSURL=plus.google.com/_/favicon?domain=www.microsoft.com}Chalice
dataWithContentsOfURL is not recommended by apple. It is a synchronous process, and blocks the execution for slow internet.Phenomenon
can you plz tell me how can i get title also.Donitadonjon
E
3

If you want the favicon, try calling this URL: http://www.google.com/s2/favicons?domain=<rss_domain> from within your app:

[NSURLConnection connectionWithRequest:
    [NSURLRequest requestWithURL:
        [NSURL URLWithString:@"http://www.google.com/s2/favicons?domain=google.com"]]
                              delegate:self];

Otherwise, an RSS channel's metadata has an optional element, <image>, which is described here: http://www.rssboard.org/rss-specification#ltimagegtSubelementOfLtchannelgt

For example:

<channel>
    <language>en-us</language>
        <title>Scientific American - News</title>
            <image>
                <title>Scientific American</title>
                <link>http://www.scientificamerican.com</link>
                <width>144</width>
                <url>
                    http://www.scientificamerican.com/media/logo/SAlogo_144px.gif
                </url>
                <height>45</height>
            </image>
        ...

This image will typically be larger than a site's favicon, and likely not-square, but with some clever cropping and scaling, it can work as an icon if a feed's favicon isn't available.

Erne answered 2/2, 2012 at 23:50 Comment(0)
G
-8

If you save the image to your desktop,

1) drag image into xcode 2)Go to interface builder 3)Go to the identity inspector after selecting the UIImage 4)Under the image drop down box, select the name of your image.

Hope that helps!

Gascony answered 3/2, 2012 at 2:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.