remove images from NSHomeDirectory() in iphone - iphone

Hi i am saving images like this:
[UIImagePNGRepresentation(image) writeToFile:[self findUniqueSavePath] atomically:YES];
// Show the current contents of the documents folder
CFShow([[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingString:#"/Documents"]]);
- (NSString *) findUniqueSavePath
{
int i = 1;
NSString *path;
do
{
// iterate until a name does not match an existing file
path = [NSString stringWithFormat:#"%#/Documents/IMAGE-%d.PNG", NSHomeDirectory(), i++];
} while ([[NSFileManager defaultManager] fileExistsAtPath:path]);
[classObj updateimage];
return path;
}
I want to remove images when the new image write in the document.

you can use this code
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path=[documentsDirectory stringByAppendingPathComponent:#"image.png"];
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];

Related

Creating multiple directories in documents folder for iOS

I want to make multiple directories I do not know how. This is my code up till now .
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"tops",#"bottoms",#"right" ];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil]; //Create folder
}
I want to have 4 directories that are named tops bottoms right and left however it does not work if I do it in the above way.. Is there a way to make multiple directories using this code? or is my code wrong? Thanks!
Try this code.
To create directories as you asked.
NSArray *directoryNames = [NSArray arrayWithObjects:#"tops",#"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
}
To use your created directories anywhere in your app.
NSArray *directoryNames = [NSArray arrayWithObjects:#"tops",#"bottoms",#"right",#"left",nil];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
// This will return the folder name in the 0 position. In yours "tops"
NSString *topDirPath = [documentsDirectory stringByAppendingPathComponent:[directoryNames objectAtIndex:0]];
// This will return the file path in your "tops" folder
NSString *filePath = [topDirPath stringByAppendingPathComponent:#"MYIMAGE"];
To store an image file
NSData *imageDataToStore = UIImagePNGRepresentation(image);
[imageDataToStore writeToFile:filePath atomically:YES];
To retrieve an image file
// Convert the file into data and then into image.
NSData *imageData = [[NSData alloc] initWithContentsOfFile:filePath];
UIImage *yourImage = [UIImage imageWithData:imageData];

How to copy one image and save as diff. name in document directory

In my document directory one image is their, with name Image1.
I want copy same image with name Image2 in document directory folder.
Please, suggest me how can i do that?
refer a following code.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *imagePath1= [documentDirectory stringByAppendingPathComponent:#"image1.png"];
NSString *imagePath2= [documentDirectory stringByAppendingPathComponent:#"image2.png"];
[[NSFileManager defaultManager] copyItemAtPath:imagePath1 toPath:imagePath2 error:nil];
Get he data from first image and save the same data with another name like:
NSData *data = UIImageJPEGRepresentation(image1, 1.0);
NSString *str=#"image2.png"
NSString *fileName = [[stringPath stringByAppendingFormat:#"/"] stringByAppendingFormat:str];
NSString *stringPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
[data writeToFile:fileName atomically:YES];
I have two mathed of Fatch image and save image:
You fatch image that save in Document directory and then pass it to save image with different name:
-(UIImage *)loadImage:(NSString *)name
{
NSString* documentsDirectory=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES) objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:name];
UIImage *res = [UIImage imageWithContentsOfFile:fullPath];
return res;
}
-(void)saveImage:(UIImage *)image withName:(NSString *)name
{
NSData *data = UIImageJPEGRepresentation(image, 1);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString* documentsDirectory=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES) objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:name];
[fileManager createFileAtPath:fullPath contents:data attributes:nil];
}

how to add element in csv file each element should be inserted in new colums of that row .. in objective c

//NSString *csvString = #"S.No,Task,Date,Time";
//NSArray *csvArray=[csvString componentsSeparatedByString:#","];
// Create .csv file and save in Documents Directory.
NSArray *csvArray =[[NSArray alloc]initWithObjects:#"SNo",#"Task",#"Date",#"Time",nil];
//create instance of NSFileManager
// NSFileManager *fileManager = [NSFileManager defaultManager];
//create an array and store result of our search for the documents directory in it
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//create NSString object, that holds our exact path to the documents directory
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(#"Document Dir: %#",documentsDirectory);
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.csv", #"userdata"]]; //add our file to the path
// [fileManager createFileAtPath:fullPath contents:[csvString dataUsingEncoding:NSUTF8StringEncoding] attributes:nil]; //finally save the path (file)
CHCSVWriter * csvWriter = [[CHCSVWriter alloc] initWithCSVFile:fullPath atomic:NO];
NSInteger numberOfColumns = 4;
for (NSInteger currentIndex = 0; currentIndex < [csvArray count]; currentIndex++) {
id field = [csvArray objectAtIndex:currentIndex];
[csvWriter writeField:field];
if ((currentIndex % numberOfColumns) == (numberOfColumns - 1)) {
[csvWriter writeLine];
}
}
[csvWriter release];
CSV file basically use for making a back up of iphone contacts and it is an Excel file why do you want to make CSV file and if you want, then you have to make an array according to your CSV and for this you have to do study.... well here is code for writing a text in CSV file and save it to in documents directory....
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [path objectAtIndex:0];
NSString *FileName = [NSString stringWithFormat:#"%#/File.csv", documentsDirectory];
NSString *Content = [[NSString alloc] initWithFormat:#"%#", stringForCsv];
//stringForCsv is your string which you want to write in file.
[Content writeToFile:FileName atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];
Thank You!

iPhone Load and Save Multiple UIImages with Unique Key?

I know there are some solutions out there for saving and loading images on your iPhone. However none have quite worked for me.
For example, the link below has a great code chunk which lets you load and save a UIImage with a unique key, but I can't work out how to tell when loading an image using this code if an image under that key already exists or not.
http://www.friendlydeveloper.com/2010/02/using-nsfilemanager-to-save-an-image-to-or-loadremove-an-image-from-documents-directory-coding/
//saving an image
- (void)saveImage:(UIImage*)image:(NSString*)imageName {
NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.
NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it
NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.png", imageName]]; //add our image to the path
[fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image)
NSLog(#"image saved");
}
//removing an image
- (void)removeImage:(NSString*)fileName {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.png", fileName]];
[fileManager removeItemAtPath: fullPath error:NULL];
NSLog(#"image removed");
}
//loading an image
- (UIImage*)loadImage:(NSString*)imageName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.png", imageName]];
return [UIImage imageWithContentsOfFile:fullPath];
}
If you could help me work out that out it would be really appreciated. I need to save several images, and load them depending on if a given key already exists or not.
if u want to get images with unique key, try using UUID. This will always give u image with unique key..For example here is an example of using UUID..
- (NSString *)pathForImageFileWithPrefix:(NSString *)Prefix
{
CFUUIDRef uuid;
CFStringRef uuidStr;
uuid = CFUUIDCreate(NULL);
uuidStr = CFUUIDCreateString(NULL, uuid);
NSData *imageData = UIImageJPEGRepresentation(chosenImage, 1.0);
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
result = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#_%#.png",Prefix, uuidStr]];
[imageData writeToFile:result atomically:NO];
CFRelease(uuidStr);
CFRelease(uuid);
return result;
}
If I get your question right, you want to know whether a file with the given image name already exists? Then you can just use
bool fileExists = [NSFileManager defaultManager] fileExistsAtPath:filePath];

how to append tags in xml in iphone?

i am new developer in iphone application. i would like write a content in xml file for that, i have created xml file and write tags with element, attribute and value with some data in that xml as follows.
-(void)writexmlfile:(NSString *)data toFile:(NSString *)fileName NodeName:(NSString *)Node{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// the path to write file
NSString *fileExtension = [NSString stringWithFormat:#"%#%#",fileName,#".xml"];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileExtension];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:appFile];
if(fileExists) {
NSError *error = nil;
NSString *docStr;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// the path to write file
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:appFile];
if(fileExists)
{
NSLog(#"file exist in document");
NSData *myData1 = [NSData dataWithContentsOfFile:appFile];
if (myData1) {
docStr = [[NSString alloc] initWithData:myData1 encoding:NSASCIIStringEncoding];
XMLString = [[NSMutableString alloc]initWithString:docStr];
[self XMLString];
NSLog(#"data%#",XMLString);
}
}
// [XMLString insertString:data atIndex:index];
[XMLString appendFormat:#"<%#>%#</%#> \n",Node,data,Node];
BOOL success = [XMLString writeToFile:appFile atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (!success) {
NSLog(#"Error: %#", [error userInfo]);
}
else {
NSLog(#"File write is successful");
}
}
}
i want add new tag with new elements,new attributes,etc.,if i enter element at the place of tag it is modifying with previous tag .
here how can i append the new tag to previous appended tags
please any body could help me
Thanks in advance
As a suggestion, you can use APXML to create XML documents it is very simple and easy to use. This post can guide you further.