Photo replaces when save to Directory - ios

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"];
NSData *imageData = UIImagePNGRepresentation(captureImage.image);
[imageData writeToFile:filePath atomically:YES];
}
to save however it always replaces itself so I can't save multiple photo's. How should I make it so that it will stop doing that without duplicating itself?

You're always saving to the same place, per this code:
NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:#"hats"]; // "right" is at index 2, per comments & code
NSString *filePath = [folderPath stringByAppendingPathComponent:#"IMAGE_NAME_HERE.PNG"];
NSData *imageData = UIImagePNGRepresentation(captureImage.image);
[imageData writeToFile:filePath atomically:YES];
Looping through the array just saves everything to the 'hats' path every time. You're already calculating the place to store the image based on the array object, through the dataPath object. You just aren't doing anything with that. Instead, do this:
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
[imageData writeToFile:dataPath atomically:YES];
}

Related

Image is duplicated while fetching the image from document directory

I'm using scrollview for image loading.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
dataPath = [documentsDirectory stringByAppendingPathComponent:#"/Images"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
NSArray *docsArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dataPath error:&error];
for (int i = 0; i < [docsArray count]; i++)
{
NSString *strPath = [docsArray objectAtIndex:i];
if([strPath containsString:[NSString stringWithFormat:#"%#_",strID]])
{
[imgArray addObject:[UIImage imageWithContentsOfFile:[NSString stringWithFormat:#"%#/%#",dataPath,strPath]]];
NSLog(#"%#%#",dataPath,strPath);
}
}
if([imgArray count]>0)
[self showImages];

Writing and retrieving files from NSDocumentDirectory iOS

I want to download a file(pdf, doc, docx etc) in NSFileManager from some specific URL. Later on when user taps on same link I want to retrieve the file from NSFileManager. Here is the code I've done:
- (void)checkIfFileExist {
NSString *path1;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
path1 = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"Content-Directory"];
path1 = [path1 stringByAppendingPathComponent:#"sample.pdf"];
NSLog(#"The read path is %#", path1);
if ([[NSFileManager defaultManager] fileExistsAtPath:path1])
{
//File exists
NSData *file1 = [[NSData alloc] initWithContentsOfFile:path1];
if (file1)
{
}
}
else
{
NSURL *url = [NSURL URLWithString:#"http://fzs.sve-mo.ba/sites/default/files/dokumenti-vijesti/sample.pdf"];
NSData *urlData = [NSData dataWithContentsOfURL:url];
NSString *path;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"Content-Directory"];
path = [path stringByAppendingPathComponent:#"sample.pdf"];
NSLog(#"The written path is %#", path);
[[NSFileManager defaultManager] createFileAtPath:path
contents:urlData
attributes:nil];
}
}
Now here every time I'm trying to fetch the file from file manager the condition that file exists always returning me FALSE. The paths where I'm writing and reading the file are same as I've checked them on console. Any mistake that I've done?
Add this
if (![[NSFileManager defaultManager] fileExistsAtPath:path]){[[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:nil];}
after
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"Content-Directory"];
in your else part and replace
[[NSFileManager defaultManager] createFileAtPath:path contents:urlData attributes:nil];` with `[urlData writeToFile:path atomically:YES];

How to call an directory when saving

II have created multiple directories like this
NSArray *directoryNames = [NSArray arrayWithObjects:#"hats",#"bottoms",#"right",#"left",nil];
however I do not know how to save into a specific directory.I save like this
NSString *filePath = [folderPath stringByAppendingPathComponent:#"IMAGE_NAME_HERE.PNG"];
NSData *imageData = UIImagePNGRepresentation(captureImage.image);
[imageData writeToFile:filePath atomically:YES];
However I do not know how to specify the directory. I only want the user to save into directory hats. How can I do this?
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *imageCacheDirPath = [docsPath stringByAppendingPathComponent:#"hats"];
NSString *imageCachePath = [imageCacheDirPath stringByAppendingPathComponent:#"IMAGE_NAME_HERE.PNG"];
if (![[NSFileManager defaultManager] fileExistsAtPath: imageCacheDirPath])
[[NSFileManager defaultManager] createDirectoryAtPath:imageCacheDirPath
withIntermediateDirectories:NO
attributes:nil
error:NULL];
[[NSFileManager defaultManager] createFileAtPath:imageCachePath
contents:dataObj
attributes:nil];

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];
}
Use timestamp value as filename to avoid duplicates

How can I make a folder in NSDocument directory where I can save all the photos and delete the data from same folder when I want?

I have a tableView where for each cell I'm getting an image url string and I'm saving all data in the NSDocument directory.
I want to store all the data in folder made in the NSDocument directory and I also want to delete all the contents in that folder later. How to do it?
this is the method I'm using
(void)setThumbnailTo :(UITableViewCell *)cell withRefString:(NSString *)string {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"patientPhotoFolder"];
NSString *imgPath = [string stringByReplacingOccurrencesOfString:#"amp;" withString:#""];
imgPath = [imgPath stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *key = [self sha1:imgPath];
NSString *fileToSave = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.jpg",key]];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:fileToSave];
if (!fileExists)
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0ul);
dispatch_async(queue, ^{
NSData *imageData=[NSData dataWithContentsOfURL:[NSURL URLWithString:imgPath]];
UIImage *image = [[UIImage alloc] initWithData:imageData];
NSData *jpegData = UIImageJPEGRepresentation(image,4);
[[NSFileManager defaultManager] createFileAtPath:fileToSave contents:jpegData attributes:nil];
dispatch_sync(dispatch_get_main_queue(), ^{
patientPhoto.image = image;
});
});
patientPhoto.image = [UIImage imageNamed:#"patientPhoto.png"];
}else{
UIImage *image = [UIImage imageWithContentsOfFile:fileToSave];
if(!image) image = [UIImage imageNamed:#"patientPhoto.png"];
patientPhoto.image = image;
}
}
How to lazily create a folder named "patientPhotoFolder" in your Documents directory (this could for example be a category on NSFileManager:
- (NSString *)pathToPatientPhotoFolder {
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES) lastObject];
NSString *patientPhotoFolder = [documentsDirectory stringByAppendingPathComponent:#"patientPhotoFolder"];
// Create the folder if necessary
BOOL isDir = NO;
NSFileManager *fileManager = [[NSFileManager alloc] init];
if (![fileManager fileExistsAtPath:patientPhotoFolder
isDirectory:&isDir] && isDir == NO) {
[fileManager createDirectoryAtPath:patientPhotoFolder
withIntermediateDirectories:NO
attributes:nil
error:nil];
}
return patientPhotoFolder;
}
How to delete the contents of that folder (given you implemented it as a category on NSFileManager):
NSString *patientPhotoFolder = [[NSFileManager defaultManager] pathToPatientPhotoDirectory];
NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:patientPhotoFolder error:nil];
for (NSString *filename in contents) {
[[NSFileManager defaultManager] removeItemAtPath:[patientPhotoFolder stringByAppendingPathComponent:filename] error:NULL];
}

Resources