Question

I'm trying to save an image to the disk every time a save button is pressed. This is the code I'm using:

NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:@"piclogs"];

[pdfData writeToFile:documentDirectoryFilename atomically:YES];

Now, obviously the file to be saved will overwrite the previously saved file every time this method is called. I'm trying to find a way to store every picture taken in there, rather than have it overwritten every time. Does anyone know if there is a simple solution to achieve this? I browsed through the docs but couldn't find anything relevant.

Was it helpful?

Solution

Use NSUUID or CFUUIDCreate to create a unique name that you can use for the file.

OTHER TIPS

or you could check if the file with that name exists and if it does don't write to file. Then change the name if you want so you do have a copy of what you may or may not be trying to save.

NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:@"piclogs"];
if (![NSURL fileURLWithPath:[self documentsPathForFileName:documentDirectoryFilename]]) {
    [pdfData writeToFile:documentDirectoryFilename atomically:YES];
} else {
    NSString* altName = [NSString stringWithFormat:@"%@-%d", documentDirectoryFilename, intValue];
    [pdfData writeToFile:altName atomically:YES];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top