【问题标题】:Get Thumb image and title of particular Website URL in iOS?在 iOS 中获取特定网站 URL 的拇指图像和标题?
【发布时间】:2016-07-17 06:42:22
【问题描述】:

我必须在 iOS 中显示缩略图并获取与 LinkedIN 应用和 Skype 应用相同的 WEB URL 标题?

有什么解决办法吗?

【问题讨论】:

标签: ios objective-c linkedin skype


【解决方案1】:

您需要下载网站的 html,然后解析它以找到 <title> 标记(它为您提供标题)

<link rel="icon" ... > 标签,它为您提供了 Favicon。 Wikipedia article on Favicon link

请注意,标题和网站图标都不能保证存在,尽管大多数网站确实提供了标题,并且大多数主要网站都提供了网站图标。

【讨论】:

    【解决方案2】:

    这是找到 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 库,它可以帮助加载您想要的图像。

    URLEmbeddedView

    Here You can check actual work of library.

    【讨论】:

    • 此 URL 仅提供最喜欢的小图标 12X12。我需要大图标,它的描述与 Linkedin 应用和 Skype 应用相同。
    • @Parthpatel1105 我更新了我的答案,请查看。
    • 感谢您提供此信息。这和我需要的完全一样。但我需要 Objective C,它是 Swift 的。
    • @Parthpatel1105 同样的项目也可用于目标 C..github.com/szk-atmosphere/URLEmbeddedView
    • @Parthpatel1105 我编辑了答案以显示 swift 版本!
    【解决方案3】:
    1. 下载主页
    2. 解析元属性 比如 og:image 和 og:url
    3. 他们的内容正是您所需要的。

    【讨论】:

    • 实际上有些网站给出了正确的 URL,但有些没有给出。那么你有正确的代码吗?
    • 他们中的大多数都提供 因此,如果您没有找到 og:image,这将确保您安全备份。
    【解决方案4】:

    其他答案是正确的,但值得注意的是,图标是 16x16 像素32x32 像素。这可能不足以满足您希望达到的尺寸和分辨率。

    没有教科书式的方法来实现这一点,但一种方法是向 google 发送网站标题 +“图标”(例如“Facebook 图标”)的请求,然后选择第一个结果。

    至于网站的标题,如果你在寻找进入网站时浏览器中显示的标题,你必须解析html寻找<title> </title>标签。

    实现这两个目标可能具有挑战性,但这绝对不是不可能的。可能有某种服务可以提供这个,但据我所知。

    【讨论】:

      【解决方案5】:

      您可以参考:“用于显示网页预览信息的 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
                      }
                  }
              })
          }
      

      【讨论】:

      • 但是有些网址没有返回图片。像谷歌,flipkart,youtube。而且我还需要Objective C中的这段代码。
      【解决方案6】:

      我试过这个库,它适用于 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
      

      【讨论】:

      • 能否请您解释一下将 MTDURLPreview 添加到项目中的步骤?
      【解决方案7】:

      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
      

      【讨论】:

      猜你喜欢
      • 2019-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多