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

标签 ios swift xcode wkwebview

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

相关文章:

ios - UITableViewCell 在运行时看起来与 InterfaceBuilder 中不同(在 iPad 上)

objective-c - 设置背景图像尺寸 IOS

ios - 将iOS应用崩溃报告从库发送到服务器

android - 将 webrtc 用于混合应用程序

ios - 如何将 UIButton 设置为选中和禁用

ios - ABAddressBookRef 在 iOS 中泄漏

ios扩展和获取url标题和页面截图

ios - (ios10.2)(swift3) 如何判断用户是否点击了本地通知?

ios - 在 Firestore 中获取数据

ios - 用户个人资料图片是 jpg 还是 png?