Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get image from documents directory swift

Say I were using this code to save an image to the documents directroy

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)     }   } } 

How would I then retrive it? Keeping in mind than in iOS8 the exact path changes often

like image 266
Learnin Avatar asked Mar 12 '15 08:03

Learnin


People also ask

What is document directory in Swift?

In the iOS application we have to store files, images, audios, videos. We can do it by creating folder in Document directory. Document directory stores user data to the given path in the apps. You can read apps data from the path of document directory.


2 Answers

You are finding the document directory path at runtime for writing the image, for reading it back, you can use the exact logic:

Swift 3 and 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          }      } } 
like image 135
Midhun MP Avatar answered Sep 27 '22 17:09

Midhun MP


Better as an extension.

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)     } } 

Used like this:

let path = URL.urlInDocumentsDirectory(with: filename).path let image = UIImage(contentsOfFile: path) 
like image 41
Reid Avatar answered Sep 27 '22 16:09

Reid