从文档目录swift获取图像

时间:2015-03-12 08:56:07

标签: ios swift nsdocumentdirectory-ios8

假设我使用此代码将图像保存到文档目录

let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory
let nsUserDomainMask = NSSearchPathDomainMask.UserDomainMask
if let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true) {
if paths.count > 0 {
    if let dirPath = paths[0] as? String {
        let readPath = dirPath.stringByAppendingPathComponent("Image.png")
        let image = UIImage(named: readPath)
        let writePath = dirPath.stringByAppendingPathComponent("Image2.png") 
        UIImagePNGRepresentation(image).writeToFile(writePath, atomically: true)
    }
  }
}

然后我怎么回头呢?请记住,与iOS 8相比,确切的路径通常会发生变化

4 个答案:

答案 0 :(得分:86)

您正在运行时找到文档目录路径来写入图像,为了读回来,您可以使用确切的逻辑:

Swift 3和Swift 4.2

let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
let nsUserDomainMask    = FileManager.SearchPathDomainMask.userDomainMask
let paths               = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
if let dirPath          = paths.first
{
   let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent("Image2.png")
   let image    = UIImage(contentsOfFile: imageURL.path)
   // Do whatever you want with the image
}

Swift 2

let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory
let nsUserDomainMask    = NSSearchPathDomainMask.UserDomainMask
if let paths            = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
{
     if paths.count > 0
     {
         if let dirPath   = paths[0] as? String
         {
             let readPath = dirPath.stringByAppendingPathComponent("Image2.png")
             let image    = UIImage(contentsOfFile: readPath)
             // Do whatever you want with the image
         }
     }
}

答案 1 :(得分:7)

更好的扩展。

extension URL {
    static var documentsDirectory: URL {
        let documentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
        return try! documentsDirectory.asURL()
    }

    static func urlInDocumentsDirectory(with filename: String) -> URL {
        return documentsDirectory.appendingPathComponent(filename)
    }
}

像这样使用:

let path = URL.urlInDocumentsDirectory(with: filename).path
let image = UIImage(contentsOfFile: path)

答案 2 :(得分:6)

Swift 2

如果您想从Swift 2中的文档目录中获取文件:

psql -h myhost -d mydatabase -c "COPY (select E'foo\nbar') TO STDOUT WITH CSV"
"foo
bar"

希望能帮助某人

答案 3 :(得分:3)

从文件夹或目录加载多个图像。 - 斯威夫特4

这是附加到show的图像,我们想要在下面给出的代码中实现。 enter image description here

以下是从文档目录中的文件夹中查找多个图像的代码。我写了一个方法来做同样的事情。

  

在代码中,我们传递“文件夹名称”(即红色)并获取该目录的内容。作为回报,我们获得了图像名称数组。

    static func loadImagesFromAlbum(folderName:String) -> [String]{

    let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
    let nsUserDomainMask    = FileManager.SearchPathDomainMask.userDomainMask
    let paths               = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
    var theItems = [String]()
    if let dirPath          = paths.first
    {
        let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent(folderName)

        do {
            theItems = try FileManager.default.contentsOfDirectory(atPath: imageURL.path)
            return theItems
        } catch let error as NSError {
            print(error.localizedDescription)
            return theItems
        }
    }
    return theItems
}

这是给定代码的结果。 enter image description here

希望它有所帮助。

由于