我做了一个RSS阅读器,我也试图显示一个预览图像。 下面是我用什么来获取图像和不工作的唯一的事情是我需要帮助正从“ST画廊”的形象创造了良好的模式类的travelator.ro网站的模式需要帮助创建一个正则表达式模式获取图像
if item?.content != nil {
print("works until here")
let htmlContent = item!.content as NSString
var imageSource = ""
let rangeOfString = NSMakeRange(0, htmlContent.length)
let regex = try! NSRegularExpression(pattern: "(http[^\\s]+(jpg|jpeg|png|tiff)\\b)", options: .caseInsensitive)
if htmlContent.length > 0 {
let match = regex.firstMatch(in: htmlContent as String, options: [], range: rangeOfString)
if match != nil {
let imageURL = htmlContent.substring(with: (match!.rangeAt(2))) as NSString
print(imageURL)
if NSString(string: imageURL.lowercased).range(of: "feedburner").location == NSNotFound {
imageSource = imageURL as String
}
}
}
if imageSource != "" {
cell.itemImageView.setImageWith(NSURL(string: imageSource) as URL!, placeholderImage: UIImage(named: "thumbnail"))
}else {
cell.itemImageView.image = UIImage(named: "thumbnail")
}
}
非常感谢提前。 :)
使用HTML解析库。 [正则表达式无法解析HTML。](http://stackoverflow.com/a/1732454/3141234)正则表达式可识别一组常规语言。 HTML是一种上下文无关的语言,在Chomsky Hierarchy上更高。正则表达式无法识别上下文无关语言。 – Alexander