Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
webview中的iOS静态图像加载_Ios_Swift_Webview - Fatal编程技术网

webview中的iOS静态图像加载

webview中的iOS静态图像加载,ios,swift,webview,Ios,Swift,Webview,我想在webview中显示缓存目录中的本地图像。 webview无法加载它。捆绑包不包含图像 我从服务器下载图像,但是我有一个带有图像标签的本地HTML文件。我怎么做 图像所在的我的路径: /用户/adambella/Library/Developer/CoreSimulator/Devices/BFA8C179-EBC5-49A7-95F3-1ABA202F44AD/data/Containers/data/Application/746BE86B-33D7-4A6F-8C4C-0E4453F

我想在webview中显示缓存目录中的本地图像。 webview无法加载它。捆绑包不包含图像

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

图像所在的我的路径:

/用户/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)')")
function testImage(url) {
    var mapDiv = document.getElementById("map");
    mapDiv.innerHTML = "<img src=file://" + url + "/>";
}
html文件中的函数:

webview2.stringByEvaluatingJavaScriptFromString("testImage('\(path)')")
function testImage(url) {
    var mapDiv = document.getElementById("map");
    mapDiv.innerHTML = "<img src=file://" + url + "/>";
}
函数测试图像(url){
var mapDiv=document.getElementById(“map”);
mapDiv.innerHTML=“”;
}

这是Swift 4.2代码

中的基本URL可以设置图像URL

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

如果您有任何疑问,请评论

您可以添加您尝试过的代码吗?是的,我刚刚编辑了说明