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

时间:2016-12-13 09:55:30

标签: ios swift xcode wkwebview

这是我加载wkwebview的方式:

    let url = URL(fileURLWithPath: Bundle.main.path(forResource: "webviews/helloworld", ofType: "html")!)
    self.webview!.loadFileURL(url, allowingReadAccessTo: url)

在模拟器上一切正常,但在我的iPhone上试用应用程序时,未加载保存在文档目录中的下载图像。

这就是我获取图片文件网址的方式。

    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/Documents/57a8dd7255723c964658262d43c169c1

  

我和这个家伙有同样的问题:WKwebview : Cannot view app documents images in app web view iOS swift

2 个答案:

答案 0 :(得分:0)

function onOpen() {
    var ui = SpreadsheetApp.getUi();
    ui.createMenu('Eta Buddy')
        .addItem('Capitalize', 'proper')
        .addToUi();
}


function proper() {
var s = SpreadsheetApp.getActiveSheet(),
excludedCols = [2, 4, 6, 8, 10];
s.getDataRange()
    .setValues(
        s.getDataRange()
        .getValues()
        .map(function (r) {
            return r.map(function (el, i) {
               return !el ? null : (typeof el !== 'string' && el) || excludedCols.indexOf(i + 1) > -1 ? el : toTitleCase(el);
            })
        })
    )
}

function toTitleCase(str) {
    return str.replace(/\w\S*/g, function (txt) {
        return txt.charAt(0)
            .toUpperCase() + txt.substr(1)
            .toLowerCase();
    });
}

答案 1 :(得分:0)

所以我解决问题的方法是将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.")}
    }