how can we get image from local? - iphone

I store image in local by this code...
NSData *imageData = UIImagePNGRepresentation(image);
NSString *imageName = [NSString stringWithFormat:#"image.png"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
[imageData writeToFile:fullPathToFile atomically:YES];
I can see this image in my iphone simulator bundle ...but how can i get back this image ?

If you mean recreate the image from a file, you can do that very simply by using UIImage's +imageWithContentsOfFile: method:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:#"someImageName"];
UIImage *image = [UIImage imageWithContentsOfFile:fullPathToFile];

Related

Save images to NSLibraryDirectory in a particular folder

I want to save the images in NSLibraryDirectory, the code i am using is replacing image, when next image is saved.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:#"images.doc"];
UIImage *image = [arrayImages objectAtIndex:0];
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];
NSMutableArray *imag = [[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:#"1333681269.jpeg"],[UIImage imageNamed:#"image001-794043.jpeg"],[UIImage imageNamed:#"Circle.png"], nil];
for(int i=0;i<[imag count];i++)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imageName = [NSString stringWithFormat:#"savedImage%i.png",i];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:imageName];
NSData *imageData = UIImagePNGRepresentation([imag objectAtIndex:i]);
[imageData writeToFile:savedImagePath atomically:NO];
}
Each time you have to use a different name that isn't already used.So You have to change the file name Each time.
If you want work it even after app restarted ,Save the index value to NSUserDefaults
.h file
int index;
Change your code as in .m
index = [[NSUserDefaults standardUserDefaults] integerForKey:#"HighScore"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"NewFolder"];
NSString *dynamicimagename = [[#"/images" stringByAppendingFormat:#"%d",[[NSUserDefaults standardUserDefaults] integerForKey:#"HighScore"]] stringByAppendingFormat:#".doc"];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:dynamicimagename];
UIImage *image = [arrayImages objectAtIndex:0];
NSData *imageData = UIImagePNGRepresentation(image);
BOOL isWritten = [imageData writeToFile:savedImagePath atomically:NO];
index++;
[[NSUserDefaults standardUserDefaults] setInteger:index forKey:#"HighScore"];

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

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

Problem writing NSData in my device

Can someone explain me why my code doesn't work?
//get the documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the
//documents directory:
NSString *fullFileName = [NSString stringWithFormat:#"%#/subscriptions", documentsDirectory];
[dataReply writeToFile:fullFileName atomically:NO];
NSData *getData=[[NSData alloc]initWithContentsOfFile:fullFileName];
infact if I try
NSString *string = [[NSString alloc] initWithData:getData encoding:NSASCIIStringEncoding];
while if I load myData without save it to my device it works! (So I'm sure that my data is not empty)
Try This,
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the
//documents directory:
NSString *fullFileName = [NSString stringWithFormat:#"%#/subscriptions", documentsDirectory];
[dataReply writeToFile:fullFileName atomically:NO];
Nsdata *getData=[fullFileName dataUsingEncoding:NSUTF8StringEncoding];

saving data in iphone

I am trying to download an image and audio file from the url.. i want to save the downloaded files in specific path is it possible..
please suggest me...
thanks...
save the files in the documents directory of your app.
NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [documentsDirectoryPath stringByAppendingPathComponent:#"myfile.mp3"];
BOOL success = [myMp3Data writeToFile:path atomically:YES];
Yes, it is possible.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath2 = [NSString stringWithFormat:#"%#/%#.jpg", documentsDirectory, fileName];
NSData* imageData = UIImageJPEGRepresentation(drawImage.image, 1.0);
[imageData writeToFile:filePath2 atomically:NO];