2

I want to put a time stamp so that when I make the app save I can avoid duplicates I am using the code

NSArray *directoryNames = [NSArray arrayWithObjects:@"hats",@"bottoms",@"right",@"left",nil];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder

    for (int i = 0; i < [directoryNames count] ; i++) {
        NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:[directoryNames objectAtIndex:i]];
        if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
            [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil]; //Create folder

        NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:@"hats"]; // "right" is at index 2, per comments & code
        NSString *filePath = [folderPath stringByAppendingPathComponent:@"IMAGE_NAME_HERE.PNG"]; // you maybe want to incorporate a timestamp into the name to avoid duplicates
        NSData *imageData = UIImagePNGRepresentation(captureImage.image);
        [imageData writeToFile:filePath atomically:YES];
    }
} 

How can I put a time stamp or something else?I just want to stop the duplicate so it dosent need to be a timestamp

Shouri
  • 39
  • 6

3 Answers3

3

You can directly use the timestamp, and there is no chance of getting it duplicate. Check the code below

time_t unixTime = (time_t) [[NSDate date] timeIntervalSince1970];
NSString *timestamp=[NSString stringWithFormat:@"%ld",unixTime];
iphonic
  • 12,615
  • 7
  • 60
  • 107
  • What happens if you don't want any time and just want it to stop duplications like a count up or something – Shouri Aug 17 '13 at 06:25
  • YES you can write images by name of counter say 1,2,3,4... .png, and can get file name by fetching file lists from the directory and finding the greatest value.. Like you determine MAX in databases – iphonic Aug 17 '13 at 06:54
  • how can you do that? I want to do that – Shouri Aug 17 '13 at 06:54
0

You didn't make it clear but I suspect what you want is to add a date as part of the filename.

To do this, you need to use a "NSDateFormatter" to convert a NSDate object (e.g. today's date?) into a string. Here is a related question that shows how to create that string.

If you don't want to make the date as part of the file or folder name, you can also simply check to see if the file already exists within the folder path. If it does exist, don't write the png file.

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • I will share more of my codes what happends if you don't want the date and just want to stop the duplicate – Shouri Aug 17 '13 at 06:21
0

You can use this NSDate selector:

- (NSTimeInterval)timeIntervalSince1970

It returns the number of seconds since 00:00:00 GMT January 1, 1970.

Example:

time_t getTime = (time_t) [[NSDate date] timeIntervalSince1970];

Which you could store so that you'd have a reference to when a file was saved.

geekchic
  • 2,371
  • 4
  • 29
  • 41
  • What happens if you don't want any time and just want it to stop duplications like a count up or something – Shouri Aug 17 '13 at 06:24
  • Well since you have the name of the image, you can always run a check to see if its already been saved. Basically an if condition. – geekchic Aug 17 '13 at 06:28
  • yes it is saved but it just keeps replacing itself when taking I just want to stop that. – Shouri Aug 17 '13 at 06:41