0

In My app I'm saving images to document directory.

My Question is if when device memory is full i.e ex if my device is 16GB and my free space is 2mb then how to handle this while saving image to document directory using WriteToFile API . [imageData writeToFile:captureImagename atomically:YES];

Any Idea to handle this condition Thanks in Advance

naga rocks
  • 93
  • 1
  • 7

2 Answers2

1

Use writeToFile:options:error: instead of writeToFile:atomically: so you can get an NSError object in case the operation fails.

Then you can check if the operation returned an error

NSError *error;
if (![plistData writeToFile:file options: NSDataWritingAtomic error:&error]) {
    NSLog(@"Error writing file: %@", error);
}
Lefteris
  • 14,002
  • 2
  • 52
  • 90
0

Trying to improve Lefteris' solution is too check if there is space available in the first place.

That way, you can ask the user what he wants to do "Cancel" or "Save (anyway)", and he knows he's saving a 3mb picture in a 2mb storage it's probably gonna fail.

Then he can free space if necessary ; always give the user choice & information, he knows better than you what's happening anyway.

According to this answer : https://stackoverflow.com/a/8036586/3603502

you can get the available disk space. Translate it in megabytes if necessary, compare with picture size, and if the difference is less than, say, 100x, show error message saying "are you sure". Then he can do it or not, and if it fails you can show error like Lefteris suggested. And I'll insist on what he said because he was right :

You should always manipulate files with error statements so you can come back with information if it fails for any reason. (and display to user if necessary).

Community
  • 1
  • 1
Gil Sand
  • 5,333
  • 3
  • 29
  • 66