0

I'm trying to create a method to download image from internet.

It download a bunch of image by NSData, it's work but I have to save it in the device. The problem is that it download perfectly and I can access to the image but if I delete the app or close the running bg app I can't access to the image, it's like it disappear. So it only work if I download and access to the image immediately.. it isn't permanent.

Here is my code:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
        NSString *newDirectory = [NSString stringWithFormat:@"%@/toyotacotizador", [paths objectAtIndex:0]];
        NSString* path = [newDirectory stringByAppendingPathComponent:
                          [NSString stringWithFormat:@"image-appdownloader-%d-%d.png", tablePointer,tagDictionary] ];
        NSData* data = UIImagePNGRepresentation(image);
        [data writeToFile:path atomically:YES];
        [self.dbManager loadDataFromDB:[NSString stringWithFormat:@"UPDATE %@ SET image='%@' WHERE %@ = '%@';", [dataImage getTableName], path, [dataImage getIdTableColumn], [dataImage getIdQuery]]];

The route of the image is correct but I can't access to the image if I close the entire app process or delete the app.

It's like the image is only visible when I download it.

Kara
  • 6,115
  • 16
  • 50
  • 57
Xeijin
  • 75
  • 1
  • 7

1 Answers1

1

You probably want to take a good read at NSData reference.

According to the reference:

The atomic write begins by writing the data to a temporary file. If this write succeeds, then the method moves the temporary file to its final location.

So the reason you don't see your images when you close your app is probably because the files haven't been moved to their final location yet.

Also, the reference explicitly says that you should not use this method:

Instead initialize an NSFileHandle object with an existing file descriptor and use the NSFileHandle methods to securely write the file.

Kara
  • 6,115
  • 16
  • 50
  • 57
Dat Nguyen
  • 1,626
  • 22
  • 25