-1

Trying to save an image as below to local storage.

if let imageData: Data = UIImagePNGRepresentation(self.emitterImageView.image!) {
         try! imageData.write(to: savedImagePath, options: [])
 }

But crashes due to following error.

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

Where is the issue?

saved image path is file:/private/var/mobile/Containers/Data/Application/41D876EC-7D65-4AB1-820E-EDE46FAE8675/tmp/PHDEmitterBackup/PHDEmitterImage1.png

Custom path

func emitterBackupDirectoryPath() -> 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
}
isuru
  • 2,640
  • 3
  • 17
  • 48
  • 4
    The error means what it says: `savedImagePath` is invalid. – vadian May 02 '19 at 04:57
  • file:/private/var/mobile/Containers/Data/Application/41D876EC-7D65-4AB1-820E-EDE46FAE8675/tmp/PHDEmitterBackup/PHDEmitterImage1.png this is the savedImagePath – isuru May 02 '19 at 05:16
  • @isuru You allow to save anything in document directory which is sandbox for your app in terms of storing. So you should store it there – Prashant Tukadiya May 02 '19 at 05:27
  • @Prashant Tukadiya If I use document directory returns same error. – isuru May 02 '19 at 05:36
  • Replace `absoluteString` with `path`. – rmaddy May 02 '19 at 05:55
  • And never have an empty `catch`. At least add `print(error)`. If you had done that you have seen the problem much sooner. – rmaddy May 02 '19 at 05:58
  • @isuru Check the rmaddy's comment if you really want to solve your issue – Prashant Tukadiya May 02 '19 at 06:04
  • @Prashant Tukadiya Yes I got error from catch block `Error Domain=NSCocoaErrorDomain Code=4 "The file “PHDEmitterBackup” doesn’t exist." UserInfo={NSFilePath=file:///var/mobile/Containers/Data/Application/8F4965F4-819B-45A1-96BC-2A77DC97993C/Documents//PHDEmitterBackup, NSUnderlyingError=0x108ebdb20 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}` – isuru May 02 '19 at 06:27
  • Solved the issue. The problem is with the dataPath variable is a URL. To get the path as a string, need to use the path property, not absoluteString. – isuru May 02 '19 at 07:18

1 Answers1

4

Solve the issue helping above comments and I state it here because it may help to someone. The problem is with the dataPath variable is a URL. To get the path as a String, need to use the path property, not absoluteString.

isuru
  • 2,640
  • 3
  • 17
  • 48