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
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)
-(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];
}
}
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];
use this line of code to give name.
[NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterShortStyle]]
This works for me for same problem.
-(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];
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.