无法从设备上的`Documents`目录加载图像

问题描述 投票:0回答:1

在模拟器上有效。这是我在保存图像时打印的路径:/var/mobile/Containers/Data/Application/31912F79-EB94-4DB1-84B8-68C7368B9160/Documents/t123t.jpg

[这是html中的img标签,应将图像加载到webView中(在模拟器上工作正常):img src="file:///var/mobile/Containers/Data/Application/31912F79-EB94-4DB1-84B8-68C7368B9160/Documents/t123t.jpg">

[我在这里看到的一切看起来都很好,我认为问题应该出在路径上,我从file://中删除了img src部分,但没有帮助我。

ios swift image webview ios12
1个回答
0
投票

首先要提到的路径

/var/mobile/Containers/Data/Application/31912F79-EB94-4DB1-84B8-68C7368B9160/Documents/t123t.jpg

带有随机字符串的文件夹(内部应用程序)在重新打开应用程序后始终会更改。

Apple出于安全目的实施此操作。

            let documentDirectory = FileManager.SearchPathDirectory.documentDirectory
            let userDomainMask = FileManager.SearchPathDomainMask.userDomainMask
            let paths = NSSearchPathForDirectoriesInDomains(documentDirectory, userDomainMask, true).first
            let path: String = paths!
            //path has address of document directory and we need address of tmp directory where pictures are stored

            let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent("\(url)")
            let image = UIImage(contentsOfFile: imageURL.path)

当我遇到相同问题时,这在我的第一个项目中为我工作。

希望我帮助了您:)

快乐编码

© www.soinside.com 2019 - 2024. All rights reserved.