Saturday, August 17, 2013

Counting up when saving

Counting up when saving

I am saving an image however it is just duplicating itself since I only
set only one name however I want it to count up so that it wont replace
itself. Is there any way to do this?
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:@"hats.PNG"]; // you maybe want to
incorporate a timestamp into the name to avoid duplicates
NSData *imageData = UIImagePNGRepresentation(captureImage.image);
[imageData writeToFile:filePath atomically:YES];
}

No comments:

Post a Comment