在 Documents 目录中加载下载的图像(IOS 10,xcode 8.1,Swift 3.0)

Loading downloaded images in Documents directory (IOS 10, xcode 8.1, Swift 3.0)

这是我加载 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

这会 return 像这样:file:///Users/Joshua/Library/Developer/CoreSimulator/Devices/9FE87399-6EBD-4DF3-BC6A-FD844DF62833/data/Containers/Data/Application/C1E250A4-823E-4590-8BDE-3891666CA728/Documents/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")
        }
    }

所以我解决这个问题的方法是将webviews目录转移到/Libraries目录并将我下载的照片存储到/Libraries/Caches

    let functionName:String = "moveFileToLibrary"
    let library = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask)[0]
    let bundle = Bundle.main.path(forResource: "webviews", ofType: "")!

    let fileManager = FileManager.default

    if fileManager.isWritableFile(atPath: library.path)
    {
        if self.debug {print("\(functionName) @ \(self.className) => \(library.path) : File is writable")}
    }
    else
    {
        if self.debug {print("\(functionName) @ \(self.className) => \(library.path) : File is read-only")}
    }

    if fileManager.isWritableFile(atPath: bundle)
    {
        if self.debug {print("\(functionName) @ \(self.className) => \(bundle) : File is writable")}
    }
    else
    {
        if self.debug {print("\(functionName) @ \(self.className) => \(bundle) : File is read-only")}
    }

    if !fileManager.fileExists(atPath: library.appendingPathComponent("webviews").path)
    {
        do
        {
            try fileManager.copyItem(atPath: bundle, toPath: library.appendingPathComponent("webviews").path)
            if self.debug {print("\(functionName) @ \(self.className) => Webviews folder copied!")}
        }
        catch let error
        {
            if self.debug {print("\(functionName) @ \(self.className) => Error Writing Webviews folder: \(error.localizedDescription)")}
        }
    }
    else
    {
        if self.debug {print("\(functionName) @ \(self.className) => Webviews folder exists. Continue woth life.")}
    }