这就是我加载 wkwebview 的方式:
let url = URL(fileURLWithPath: Bundle.main.path(forResource: "webviews/helloworld", ofType: "html")!)
self.webview!.loadFileURL(url, allowingReadAccessTo: url)
在模拟器上一切正常,但在我的 iPhone 上尝试该应用程序时,未加载保存在文档目录中的下载图像。
这就是我获取图像文件 URL 的方法。
let fileManager = FileManager.default
let directoryURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
let pathComponent = filename
return directoryURL.appendingPathComponent(pathComponent).absoluteString
这将返回如下内容:file:///Users/Joshua/Library/Developer/CoreSimulator/Devices/9FE87399-6EBD-4DF3-BC6A-FD844DF62833/data/Containers/Data/Application/C1E250A4- 823E-4590-8BDE-3891666CA728/文档/57a8dd7255723c964658262d43c169c1
I have the same problem as this guy: WKwebview : Cannot view app documents images in app web view iOS swift
最佳答案
func getDirectoryPath() -> String {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0]
return documentsDirectory
}
func getImage(){
let fileManager = NSFileManager.defaultManager()
let imagePAth = (self.getDirectoryPath() as NSString).stringByAppendingPathComponent("yourImageName.jpg")// Your image name must be same as when you save image in directory
if fileManager.fileExistsAtPath(imagePAth){
self.imageView.image = UIImage(contentsOfFile: imagePAth)
// or do whatever you want with image.
}else{
print("No Image")
}
}
关于ios - 在 Documents 目录中加载下载的图像(IOS 10、xcode 8.1、Swift 3.0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41118107/