【发布时间】:2016-07-17 06:42:22
【问题描述】:
【问题讨论】:
-
这个问题太笼统了,缺乏和MCVE。问题陈述也不清楚。你想要的“拇指图”是什么?
标签: ios objective-c linkedin skype
【问题讨论】:
标签: ios objective-c linkedin skype
您需要下载网站的 html,然后解析它以找到 <title> 标记(它为您提供标题)
和<link rel="icon" ... > 标签,它为您提供了 Favicon。
Wikipedia article on Favicon link
请注意,标题和网站图标都不能保证存在,尽管大多数网站确实提供了标题,并且大多数主要网站都提供了网站图标。
【讨论】:
这是找到 URL 的 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 库,它可以帮助加载您想要的图像。
【讨论】:
【讨论】:
其他答案是正确的,但值得注意的是,图标是 16x16 像素 或 32x32 像素。这可能不足以满足您希望达到的尺寸和分辨率。
没有教科书式的方法来实现这一点,但一种方法是向 google 发送网站标题 +“图标”(例如“Facebook 图标”)的请求,然后选择第一个结果。
至于网站的标题,如果你在寻找进入网站时浏览器中显示的标题,你必须解析html寻找<title> </title>标签。
实现这两个目标可能具有挑战性,但这绝对不是不可能的。可能有某种服务可以提供这个,但据我所知。
【讨论】:
您可以参考:“用于显示网页预览信息的 NSURL 扩展” 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
}
}
})
}
【讨论】:
我试过这个库,它适用于 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
【讨论】:
URLEmbeddedView 用 Swift 编写,但您可以在 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
【讨论】: