4

In my ios app i am stuck on a task. I need to take pic from camera and save it on document directory.Problem is that i want save unique name of image.I was try to add current time with a name. but there are length problem to save image.Please suggest me how can i do that task.

Thanks

rmaddy
  • 314,917
  • 42
  • 532
  • 579
M.B
  • 885
  • 2
  • 15
  • 31

5 Answers5

7

Given a proposed name like NString *name = @"Lake":

NSString *myUniqueName = [NSString stringWithFormat:@"%@-%u", name, (NSUInteger)([[NSDate date] timeIntervalSince1970]*10.0)];

EDIT: updated so that the only chance of a duplicate is the same original name, submitted within 100 ms of the first (virtually impossible in my opinion, if this is a concern use 100 instead of 10)

David H
  • 40,852
  • 12
  • 92
  • 138
  • That's only unique if a file is saved slower than once per second. I can see overwriting occurring pretty frequently... – trojanfoe Jun 05 '13 at 13:46
  • @trojanfoe see revised solution – David H Jun 05 '13 at 15:12
  • I was thinking an incrementing (`static`) sequence number would be a better solution, so the OP could create a singleton class just for this very purpose? – trojanfoe Jun 05 '13 at 15:18
  • @trojanfoe This problem has a huge number of solutions! The static sequencer is a problem when the app restarts, as it will be 0 (or whatever its intialized to). The logic would then need to figure out what the last number was. Using the date requires no saved information. – David H Jun 05 '13 at 15:20
  • Restarting the app shouldn't be a problem as long as it doesn't restart within a second. – trojanfoe Jun 05 '13 at 15:22
3
-(NSString*)getFilePathToSaveUnUpdatedImage
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *directory = [paths objectAtIndex:0];

    for (int i = 0 ; TRUE ; i++)
    {
        if(![[NSFileManager defaultManager]fileExistsAtPath:[NSString stringWithFormat:@"%@/UnUpdatedItems/Image%d.png", directory , i]])
            return [NSString stringWithFormat:@"%@/UnUpdatedItems/Image%d.png", directory , i];
    }
}
Alok Singh
  • 896
  • 6
  • 18
3

Try like this just formate the date and save the image

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];

[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSDate *now = [NSDate date];

NSString *theDate = [dateFormat stringFromDate:now];

[data writeToFile:[NSString stringWithFormat:@"%@.png",theDate] atomically:YES];
NSLog
  • 649
  • 1
  • 5
  • 12
1

use this line of code to give name.

[NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterShortStyle]]

This works for me for same problem.

0
-(NSString*)getImagePathName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *directory = [paths objectAtIndex:0];

    for (int i = 0 ; TRUE ; i++)
    {
        if(![[NSFileManager defaultManager]fileExistsAtPath:[NSString stringWithFormat:@"%@/Image%d.png", directory , i]])
            return [NSString stringWithFormat:@"%@/Image%d.png", directory , i];
    }
}
Alok Singh
  • 896
  • 6
  • 18
Amit Gupta
  • 371
  • 2
  • 9