【问题标题】:iOS static image load in webviewWebview中的iOS静态图像加载
【发布时间】:2016-04-13 08:55:21
【问题描述】:

我想在 web 视图中显示来自 CachesDirectory 的本地图像。 webview 无法加载它。捆绑包不包含图片!

我从服务器下载图像,但我有一个带有图像标签的本地 HTML 文件。我该怎么做?

我的图片所在路径:

/Users/adambella/Library/Developer/CoreSimulator/Devices/BFA8C179-EBC5-49A7-95F3-1ABA202F44AD/data/Containers/Data/Application/746BE86B-33D7-4A6F-8C4C-0E4453FE374C/Library/Caches/swift。 imageloader.diskcached/

谁能给我一个简短的例子?

我是用 ImageLoader 下载的。 带有名称的路径:

var path: String {
    let cacheDirectory = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true)[0]
    let directoryName = "swift.imageloader.diskcached"

    return cacheDirectory + "/" + directoryName
}

let filePath = NSBundle.mainBundle().pathForResource("example", ofType: "html",inDirectory: "Directory")

let urlToFile = NSURL(fileURLWithPath: filePath!)

let request = NSURLRequest(URL: urlToFile, cachePolicy: .ReturnCacheDataElseLoad, timeoutInterval: 10.0)

在 webViewDidFinishLoad 方法中:

webview2.stringByEvaluatingJavaScriptFromString("testImage('\(path)')")

html文件中的函数:

function testImage(url) {
    var mapDiv = document.getElementById("map");
    mapDiv.innerHTML = "<img src=file://" + url + "/>";
}

【问题讨论】:

  • 可以添加你试过的代码吗?
  • 是的,我刚刚编辑了描述

标签: ios swift webview


【解决方案1】:

这是 Swift 4.2 代码。

base URL in 你可以设置Image URL

web.loadRequest(URLRequest.init(url: baseURL!))

如有疑问请留言

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-30
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-09
    • 2016-12-05
    相关资源
    最近更新 更多