The file is deleted when changing the driver


I have an app where I save a file in a method called send image and there is a save and if I verify the existence of the file it returns true and everything is fine, but then according to some criteria another method must be fired in this method should look for that file that was previously saved and should be sent to a Web service but when I verify the existence of this returns false that no longer exists, is it because this last method is in another controller? Why do not I have an idea? This is the code to save the file:  let documentsPath = NSSearchPathForDirectoriesInDomains (.DocumentDirectory, .UserDomainMask, true) [0]

    // Imagen y imagen en formato NSData
    if let imageData = UIImageJPEGRepresentation(self.imgLugar.image!, 1.0) {

        // Creamos la URL final
        let filePath = NSURL(fileURLWithPath: documentsPath).URLByAppendingPathComponent(nombreImg)

        do {
            // Guardamos imagen
           try print("guardo: \(imageData.writeToURL(filePath, options: []))")
             let fileManager = NSFileManager.defaultManager()
            let imgString = fileManager.fileExistsAtPath("\(documentsPath)/\(nombreImg)")
            print("existe: \(imgString))")
            let rut = "/var/mobile/Containers/Data/Application/80420879-6E5A-4211-8014-EAFAA9273897/Documents/img_49_24"
            print("existeAnt: \(fileManager.fileExistsAtPath(rut))")
        } catch {




and this is the code in another controller where I look for the image that was previously saved in the appdelegate:

let fileManager = NSFileManager.defaultManager ()

            let imgString = fileManager.fileExistsAtPath(String(usuarioLugar.uslImagen))

the path is the following is the same in both methods: / var / mobile / Containers / Data / Application / 80420879-6E5A-4211-8014-EAFAA9273897 / Documents / img_49_24 the fileExistsAtPath in the first method returns true and in the second false. What can I do or what is this?

asked by Juan David Gil 13.06.2016 в 18:55

1 answer


To save an image use:

func save() {
    let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
    if let imageData = UIImageJPEGRepresentation(imgLugar.image!, 1.0) {

        let imageLocalURL = NSURL(fileURLWithPath: documentsPath).URLByAppendingPathComponent("img.jpeg")
        imageData.writeToURL(imageLocalURL, atomically: true)

    let existeImg = exists("img.jpeg")

And to check if an image is saved you can use:

func exists(name: String) -> Bool {
    let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
    let imageLocalURL = NSURL(fileURLWithPath: documentsPath).URLByAppendingPathComponent(name)
    var error: NSError?
    let imagenExiste = imageLocalURL.checkResourceIsReachableAndReturnError(&error)
    if imagenExiste && error == nil {
        return true
    } else {
        return false

In your code there are fixed paths (e.g. rut ) and methods that are called within a String interpolation, again within a print statement (e.g. try print("guardo: \(imageData.writeToURL(filePath, options: []))") ). Fixed and absolute paths are not a good idea because they change, use NSSearchPathForDirectoriesInDomains like in the previous example. And the methods in String interpolations are a bad idea because they are confusing, even worse within sentences print .

answered by 14.06.2016 в 00:38