title: Cannot write to local storage "No such file or directory" issue in iOS swift
body: <p>Trying to save an <code>image</code> as below to local storage. </p>
  
  <pre><code>if let imageData: Data = UIImagePNGRepresentation(self.emitterImageView.image!) {
           try! imageData.write(to: savedImagePath, options: [])
   }
  </code></pre>
  
  <p>But crashes due to following error.</p>
  
  <blockquote>
    <p>Thread 1: Fatal error: 'try!' expression unexpectedly raised an error:
    Error Domain=NSCocoaErrorDomain Code=4 "The file
    “PHDEmitterImage1.png” doesn’t exist."
    UserInfo={NSFilePath=/private/var/mobile/Containers/Data/Application/41D876EC-7D65-4AB1-820E-EDE46FAE8675/tmp/PHDEmitterBackup/PHDEmitterImage1.png,
    NSUnderlyingError=0x15bf21790 {Error Domain=NSPOSIXErrorDomain Code=2
    "No such file or directory"}}</p>
  </blockquote>
  
  <p>Where is the issue?</p>
  
  <p>saved image path is <code>file:/private/var/mobile/Containers/Data/Application/41D876EC-7D65-4AB1-820E-EDE46FAE8675/tmp/PHDEmitterBackup/PHDEmitterImage1.png</code></p>
  
  <p>Custom path</p>
  
  <pre><code>func emitterBackupDirectoryPath() -&gt; String? {
  
      let dataPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.absoluteString
      //let dataPath = URL(fileURLWithPath: documentsDir).appendingPathComponent("/PHDEmitterBackup").absoluteString
  
      //let dataPath = documentsDir?.appendingPathComponent("/PHDEmitterBackup").absoluteString
  
      if !FileManager.default.fileExists(atPath: dataPath!) { //if does not exist
          do {
              try FileManager.default.createDirectory(atPath: dataPath!, withIntermediateDirectories: false, attributes: nil) //Create folder
          } catch {
          }
      }
  
      return dataPath
  }
  </code></pre>