Skip to content

Instantly share code, notes, and snippets.

@RishabhTayal
Created April 6, 2016 18:46
Show Gist options
  • Save RishabhTayal/95b49746eb878c805bdf533aeff4ab87 to your computer and use it in GitHub Desktop.
Save RishabhTayal/95b49746eb878c805bdf533aeff4ab87 to your computer and use it in GitHub Desktop.
let urlString = "http://www.nytimes.com/2016/04/07/us/politics/donald-trump-and-hillary-clinton-look-to-rebound-in-new-york.html?_r=0"
let photoData = NSMutableData(contentsOfURL: NSURL(string: urlString)!)
let dataString = String(data: photoData!, encoding: NSUTF8StringEncoding)
do {
// <meta property=\"og:image
let regex = try NSRegularExpression(pattern: "<meta.*property=\"og:image\".*content=\"(.*)\".*\\/>", options: .CaseInsensitive)
let match = regex.firstMatchInString(dataString!, options: .WithoutAnchoringBounds, range: NSRange(location: 0, length: (dataString?.characters.count)!))
// regex.matchesInString(dataString!, options: .with, range: <#T##NSRange#>)
let img = (dataString! as NSString).substringWithRange((match?.range)!)
let httpRange = img.substringFromIndex((img.rangeOfString("http")?.startIndex)!)
let endRange = httpRange.substringToIndex((httpRange.rangeOfString(" />")?.startIndex)!)
print(endRange)
let imageD = NSData(contentsOfURL: NSURL(string: endRange)!)
let image = UIImage(data: imageD!)
} catch {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment