iPhone Load and Save Multiple UIImages with Unique Key? - iphone

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];

Related

How to save pdf locally from image view

How to save the pdf locally
I have loaded the pdf doc into imageView. Now i need to save into locally into iPhone device.
Any one advice me how to save the pdf file from image view
#All
Thanks in advance
For saving the pdf in document directory use below code.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:#"data.pdf"];
NSData *thedata = [NSData dataWithContentsOfURL:[NSURL URLWithString:`ADD URL OF PDF DATA`]];
[thedata writeToFile:localFilePath atomically:YES];
For retrieving the pdf use below code.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:#"data.pdf"];
if ( [[NSFileManager defaultManager] fileExistsAtPath:localFilePath] ) {
NSData *data = [NSData dataWithContentsOfFile:localFilePath];
}

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];
}

Saving and loading UIImage in an iPhone app

I am having a headache working out what's wrong with my app. I am trying to save and load UIImages, simple right? Seems not so.
I have a class with these methods to save, load and delete an image with a unique string. Just how I like it, nice and simple. Here is the class:
imageSavedLoad.h:
#import <Foundation/Foundation.h>
#interface imageSaveLoad : NSObject
- (void)saveImage:(UIImage*)image forKey:(NSString*)imageName;
- (void)removeImage:(NSString*)fileName;
- (UIImage*)loadImage:(NSString*)imageName;
#end
imageSaveLoad.m:
#import "imageSaveLoad.h"
#implementation imageSaveLoad
//saving an image
- (void)saveImage:(UIImage*)image forKey:(NSString*)imageName
{
NSData *imageData = UIImagePNGRepresentation(image);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.png", imageName]];
[fileManager createFileAtPath:fullPath contents:imageData attributes:nil];
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];
}
#end
Where I want to save, load or delete an image I import the class: #import "imageSaveLoad.h"
Then I create an instance of it and call the required methods (here I save):
imageSaveLoad *imgSL = [[imageSaveLoad alloc] init];
[imgSL saveImage:photoImgView.image forKey:#"yourPhoto"];
Then in another view/class where I want to retrieve the image:
imageSaveLoad *imgSL = [[imageSaveLoad alloc] init];
yourImgView.image = [imgSL loadImage:#"yourPhoto"];
But yourImgView displays nothing, blank.
So where am I going wrong? I have checked all IBOutlet connections and that all the code is being called. Is there a problem with the methods I am using?
When saving image, try by replacing this line :
[fileManager createFileAtPath:fullPath contents:imageData attributes:nil];
With
[imageData writeToFile:fullpath atomically:YES];

remove images from NSHomeDirectory() in 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];

Save Image on iPhone idle time

I want to save the loaded image from server in idle time.I use the following to to save image in iPhone memory.
- (void)saveImage:(UIImage *)image withName:(NSString *)name
{
NSData *data1 = UIImageJPEGRepresentation(image, 1.0);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:name];
[fileManager createFileAtPath:fullPath contents:data1 attributes:nil];
if (image==nil) {
NSLog(#"Not Saved...:%#",name);
}
}
but when his method called i got halt time....i don't want that...i want to save image when iPhone is inactive.....can help??
Thanks
You need to do this in the background, using a thread.
If you're supporting iOS 4.0 only, it's really easy:
- (void)saveImage:(UIImage *)image withName:(NSString *)name {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, NULL), ^{
NSData *data1 = UIImageJPEGRepresentation(image, 1.0);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:name];
[fileManager createFileAtPath:fullPath contents:data1 attributes:nil];
if (image==nil) {
NSLog(#"Not Saved...:%#",name);
}
});
}
Otherwise, look up the NSThread documentation, or performSelectorInBackground:withObject:.