【问题标题】:In Swift 3, how to display images from link to local file directory in tableviewcell在 Swift 3 中,如何在 tableviewcell 中显示从链接到本地​​文件目录的图像
【发布时间】:2017-01-08 18:38:32
【问题描述】:

我想将图像保存在本地文件中而不是服务器上,并通过指向该文件\文件夹的链接在 UItableviewcell 中显示这些图像。 我不确定我用来保存在数据库中的路径。我将图像文件夹保存在桌面中。但是该路径:/desktop/imagefolder/image.jpg 不起作用。

【问题讨论】:

    标签: swift local-storage


    【解决方案1】:

    iOS 应用程序只能访问沙盒,这意味着您只能在文件夹 Document、Temp 和 Library 中写入。 See the documentation.

    这里是一个如何在文档目录中写入和读取图像的示例。

    let documentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    let path = "\(documentsDirectory)/image.png"
    let image = UIImage(contentsOfFile: path)
    

    现在如何保存:

    let image = ...
    let documentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    let path = "\(documentsDirectory)/image.png"
    if let data = UIImagePNGRepresentation(image!) {
        try? data.write(to: URL(fileURLWithPath: path))
    }
    

    【讨论】:

      猜你喜欢
      • 2018-05-17
      • 1970-01-01
      • 2011-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-24
      相关资源
      最近更新 更多