答案 0 :(得分:5)
以下是查找网址
的favicon图标的最简单方法NSString *myURLString = @"http://www.google.com/s2/favicons?domain=www.facebook.com";
NSURL *myURL=[NSURL URLWithString: myURLString];
NSData *myData=[NSData dataWithContentsOfURL:myURL];
UIImage *myImage=[[UIImage alloc] initWithData:myData];
Swift版本:
let myURLString = "http://www.google.com/s2/favicons?domain=www.facebook.com"
let myURL = NSURL(string: myURLString)!
let myData = NSData.dataWithContentsOfURL(myURL)
let myImage = UIImage(data: myData)!
这可以帮助很多
<强>更新强>
下面是swift库,它可以帮助您按照自己的意愿加载图像。
答案 1 :(得分:1)
您可以参考:&#34; NSURL扩展程序,用于显示网页的预览信息&#34; https://www.cocoacontrols.com/controls/urlpreview
func fetchPageInfo(completion: ((title: String?, description: String?, previewImage: String?) -> Void), failure: ((errorMessage: String) -> Void)) {
let request = NSMutableURLRequest(URL: self)
let newUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36"
request.setValue(newUserAgent, forHTTPHeaderField: "User-Agent")
ValidationQueue.queue.cancelAllOperations()
NSURLConnection.sendAsynchronousRequest(request, queue: ValidationQueue.queue, completionHandler: { (response: NSURLResponse?, data: NSData?, error: NSError?) -> Void in
if error != nil {
dispatch_async(dispatch_get_main_queue(), {
failure(errorMessage: "Url receive no response")
})
return
}
if let urlResponse = response as? NSHTTPURLResponse {
if urlResponse.statusCode >= 200 && urlResponse.statusCode < 400 {
if let data = data {
if let doc = Kanna.HTML(html: data, encoding: NSUTF8StringEncoding) {
let title = doc.title
var description: String? = nil
var previewImage: String? = nil
print("title: \(title)")
if let nodes = doc.head?.xpath("//meta").enumerate() {
for node in nodes {
if node.element["property"]?.containsString("description") == true ||
node.element["name"] == "description" {
print("description: \(node.element["content"])")
description = node.element["content"]
}
if node.element["property"]?.containsString("image") == true &&
node.element["content"]?.containsString("http") == true {
previewImage = node.element["content"]
}
}
}
dispatch_async(dispatch_get_main_queue(), {
completion(title: title, description: description, previewImage: previewImage)
})
}
}
} else {
dispatch_async(dispatch_get_main_queue(), {
failure(errorMessage: "Url received \(urlResponse.statusCode) response")
})
return
}
}
})
}
答案 2 :(得分:1)
URLEmbeddedView,但您可以在Objective-C中使用。
This是Objective-C示例代码。请检查一下。
#import <URLEmbeddedView/URLEmbeddedView-Swift.h>
#import <MisterFusion/MisterFusion-Swift.h>
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (strong, nonatomic) URLEmbeddedView *embeddedView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.textView.text = @"https://github.com/";
[OGDataProvider sharedInstance].updateInterval = [NSNumber days:10];
self.embeddedView = [[URLEmbeddedView alloc] initWithUrl:@""];
[self.containerView addLayoutSubview:self.embeddedView andConstraints:@[
self.embeddedView.Top,
self.embeddedView.Right,
self.embeddedView.Left,
self.embeddedView.Bottom
]];
}
- (IBAction)didTapFetchButton:(id)sender {
[self.embeddedView loadURL:self.textView.text completion:nil];
}
@end
答案 3 :(得分:0)
你需要下载网站的html,然后解析它为<title>
标签(给你标题)
以及为您提供Favicon的<link rel="icon" ... >
标记。
Wikipedia article on Favicon link
请注意,虽然大多数网站都提供标题,但大多数网站都提供标题,而且标题和图标都不会保证存在。
答案 4 :(得分:0)
答案 5 :(得分:0)
其他答案都是正确的,但值得注意的是,favicon是 16x16像素或 32x32像素。这可能不足以达到您希望达到的尺寸和分辨率。
没有教科书方法可以实现这一点,但一种方法是向谷歌发送网站标题+&#34; icon&#34; (例如&#34; Facebook图标&#34;),然后选择第一个结果。
对于网站的标题,如果您在进入网站时查找浏览器中显示的标题,则必须解析html以查找<title> </title>
标记。
实现这两个目标可能具有挑战性,但这绝对不是不可能的。可能有某种服务可以提供这种服务,但不是我所知道的。
答案 6 :(得分:0)
我试过这个库,它适用于youtube,flipkart。
https://github.com/myell0w/MTDURLPreview
[MTDURLPreview loadPreviewWithURL:[NSURL URLWithString:@"http://www.flipkart.com"] completion:^(MTDURLPreview *preview, NSError *error) {
}];
Printing description of preview->_title:
Online Shopping India | Buy Mobiles, Electronics, Appliances, Clothing and More Online at Flipkart.com
Printing description of preview->_domain:
www.flipkart.com
Printing description of preview->_imageURL:
http://img1a.flixcart.com/www/promos/new/20160701_015447_730x300_monsoon.jpg