Kishor Pahalwani
Kishor Pahalwani

Reputation: 1022

Image is not existing in document directory if I will run the app again using Swift 3

I am saving a camera image into my document directory. I am creating a document directory in my util class. Here below is my code :-

//Get Document Directory Path
func getDirectoryPath() -> String {
  let paths = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent("Phss")
  return paths
}

//Create Directory
func createDirectory(){
  let fileManager = FileManager.default

  let paths = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent("Phss")

  if !fileManager.fileExists(atPath: paths){
    try! fileManager.createDirectory(atPath: paths, withIntermediateDirectories: true, attributes: nil)
  }

  else{
    Helper.sharedInstance.Print("Already dictionary created." as AnyObject)
  }
}   

After that I am saving image by name and some value (docAddedTime) and storing the path(imagePath) in my core data DB.

func saveImageDocumentDirectory(imageData : Data, docName : String) -> String {
    let fileManager = FileManager.default
    let imagePAth = (getDirectoryPath() as NSString).appendingPathComponent("\(String(describing: docAddedTime!) + "_" + "\(String(describing: docName))").png")

    fileManager.createFile(atPath: imagePAth, contents: imageData, attributes: nil)
    return imagePAth
}.   

I am fetching image by image path which is saved in my local core data DB.

let fileManager = FileManager.default
    let imagePAth = doc!.docPath! //Core Data DB Path of image

    if fileManager.fileExists(atPath: imagePAth) {
         imgView.image = UIImage(contentsOfFile: imagePAth
       }

The problem is first time I am able to fetch image and is showing in my imageView but after that I will run the app again and I am trying to fetch image by this imagePath which is stored in my core data DB then it's not giving that file exist at this path.

Image is present at same Path but showing is not exits. I am not sure why this is happening.

Upvotes: 1

Views: 779

Answers (2)

Vivek Kumar
Vivek Kumar

Reputation: 406

Each time you are building the app from Xcode, new folder is being created and files will be in the new folder.

Do not rebuild the app just close the app from simulator and start the app again from simulator itself.

Complete Path of your file will be like:

file:///Users/username/Library/Developer/CoreSimulator/Devices/4FA96815-521B-4D84-B5C7-10697DE1908B/data/Containers/Data/Application/54CC678C-7E96-4DB6-83CC-6ECB506DC9BF/Documents/tmp.png

On the next build:

Application/54CC678C-7E96-4DB6-83CC-6ECB506DC9BF/Documents/

will be changed as:

Application/3F3093C6-2140-473B-8F99-A717AF162CDE/Documents/

where 3F3093C6-2140-473B-8F99-A717AF162CDE is created by Xcode not you

Upvotes: 1

tendstoZero
tendstoZero

Reputation: 156

let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let url = URL(fileURLWithPath: paths).appendingPathComponent("fileName")

You can try the above snippet. This one don't create a directory though, I would suggest first try to fetch straight from Documents directory, once its a success then create a directory and then store it.Let me know the exact scenario. What is the value of the path showing when running for the next time.

Upvotes: 0

Related Questions