【发布时间】:2016-11-19 11:59:50
【问题描述】:
如何在 swift 2.2 的某些站点中显示来自 html 源的图像?
实际上我没有任何 JSON 或 XML。
重要的是我必须使用正则表达式。
我试过这个:
if htmlContent != nil
{
let htmlContent = (item?.htmlContent)! as NSString
var imageSource = ""
let rangeOfString = NSMakeRange(0, htmlContent.length)
let regex = try! NSRegularExpression(pattern: "(<img.*?src=\")(.*?)(\".*?>)", options: [.CaseInsensitive])
if htmlContent.length > 0
{
let match = regex.firstMatchInString(htmlContent as String, options: [.WithTransparentBounds], range: rangeOfString)
if match != nil
{
let imageURL = htmlContent.substringWithRange(match!.rangeAtIndex(2)) as NSString
print(imageURL)
if NSString(string: imageURL.lowercaseString).rangeOfString("feedBurner").location == NSNotFound
{
imageSource = imageURL as String
}
}
}
if imageSource != ""
{
imgHTMLLoader.setImageWithURL(NSURL(fileURLWithPath: imageSource), placeholderImage: UIImage(named: "placeholder"))
print("placeholderImage is not! nil")
}
else
{
imgHTMLLoader.image = UIImage(named: "placeholder")
print("placeholderImage is nil")
}
}
在此示例(库)中... htmlContent 始终为零。
此示例,使用“Helper library”,但它不起作用...
谢谢
【问题讨论】:
-
欢迎来到 *!你可以请张贴,你已经尝试了什么?请提供Minimal, Complete, and Verifiable example。
-
你试过UIWebView吗?
-
不...但这适用于我的应用程序吗?
-
我不想加载整个页面...我只想加载图片
-
我检查了 UIWebView...它不适用于这种情况...但感谢您的关注