Iphone : How to Display Document Directory images in Image View? - iphone

In a App images stored in Document Directory Folder,
I wanted Particular image display in Image view,
How that image Display in Image View from the Document Directory ?

sample code:
- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
-(void)loadimage{
NSString *workSpacePath=[[self applicationDocumentsDirectory] stringByAppendingPathComponent:#"your image-name"];
UIImageView *myimage=[UIImageView alloc] initWithFrame:CGRectMake(0,0,20,20)];
myimage.image=[UIImage imageWithData:[NSData dataWithContentsOfFile:workSpacePath]];
[self.view addSubView:myimage];
[myimage release];
}
TNQ

You can achieve it, By knowing the names of your images, so while you are storing your image you need to store some information of these images in database,(or some where you can use it), then fetch name of image from data source, and work as follows:
NSString *str = [NSString stringWithFormat:#"%#.jpg",[myGlobleArrayOfImageNames objectAtIndex:imageCounter]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullImgNm=[documentsDirectory stringByAppendingPathComponent:[NSString stringWithString:str]];
[mainSlideShowImageView setImage:[UIImage imageWithContentsOfFile:fullImgNm]];

Here you can take UIImageView in your View and add it to the view and do the same like as follows:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imageFilePath;
NSError *err;
imageFilePath = [documentsDirectory stringByAppendingPathComponent:#"image1.png"];
UIImageView *objImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
objImageView.image = [UIImage imageWithContentsOfFile: imageFilePath];
[self.view addSubVIew:objImageView.image];
[objImageView release];
Please let me know if you still need any help.

NSArray *directoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *imagePath = [directoryPath objectAtIndex:0];
imagePath= [imagePath stringByAppendingPathComponent:#"imagename.png"];
NSData *data = [NSData dataWithContentsOfFile:imagePath];
UIImage *img = = [UIImage imageWithData:data];
you can display this image to uiimageview

you need following lines of code to get UIImage from documents directory
-(UIImage*)getImage:(NSString *)strImageName
{
NSString *documentDirectory=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *imagePath=[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"/%#",strImageName]];
UIImage *image=[UIImage imageWithContentsOfFile:imagePath];
return image;
}
Call above function and use UIImage object to set anywhere you want.
You can also write this method in a common class to use it anywhere.

Adding to these answers I find its good practice to check if the file exists like so...
NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *file = [directory stringByAppendingPathComponent#"my_image.jpg"];
if ([[NSFileManager defaultManager] fileExistsAtPath:file]) {
self.myimageview = [UIImage imageWithData:[NSData dataWithContentsOfFile: file]];
}

Simple Use This Code
NSFileManager *filemgr =[NSFileManager defaultManager];
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *docsDir = [dirPaths objectAtIndex:0];
NSString *dataPath = [docsDir stringByAppendingPathComponent:#"/yourdirectory/image.jpg"];
NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath:dataPath]];
self.imageview.image = [[UIImage alloc] initWithData:imgData];

Related

how to display image from documents folder in of iphone to image ViewI [duplicate]

This question already has answers here:
Loading an Image from documents directory
(6 answers)
Closed 9 years ago.
I want to display image saved in the documents folder problem is that it returns file name but it does not show image in the image View
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString*patientlastName;
NSString*test=#"nicej";
patientlastName=[test stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString * filePath = [NSString stringWithFormat:#"%#/Documents//%#.png", NSHomeDirectory(),patientlastName];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
if (fileExists == 0)
{
NSLog(#"File is Not exists");
appDelegate.imageURL=#"File Not Exists";
}
NSString *userfile=[NSString stringWithFormat:#"%#",patientlastName];
appDelegate.imageURL=userfile;
PictureDisplayViewController *targetController = [[PictureDisplayViewController alloc] initWithNibName:#"PictureDisplayViewController" bundle:nil];
targetController.modalPresentationStyle = UIModalPresentationFullScreen;
[self.splitViewController presentViewController:targetController animated:YES completion:nil];
In PictureDisplayViewController
imageView.image=[UIImage imageNamed:#"nicej.png"];
try this code:
save image in this path:
NSFileManager *fileMan = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *str= [NSString stringWithFormat:#"%#.png",patientlastName];
NSString *Image_filePath=[documentsDirectory stringByAppendingPathComponent:str];
NSData *imagedata=UIImagePNGRepresentation(yourimage);
[fileMan createFileAtPath:[Image_filePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] contents:imagedata attributes:nil];
get image path:
NSString* fileName = [NSString stringWithFormat:#"%#.png",patientlastName}
NSArray *arrayPaths =
NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *path = [arrayPaths objectAtIndex:0];
NSString* pdfFileName = [path stringByAppendingPathComponent:fileName];
UIImage *image1=[UIImage imageWithContentsOfFile:pdfFileName];
imageView.image=image1;
//suppose imgpath is path of your image in doument directory then use these two lines of code.
NSData *imgData = [NSData dataWithContentsOfFile:imgPath];
imageView.image = [[UIImage alloc] initWithData:imgData];
In PictureDisplayViewController, you are loading image with imageNamed method that actually doesn't work until you have image with the same name into your bundle.
you can load image like as follow...
In PictureDisplayViewController you already have path for the image than
imageView.image= [UIImage imageWithContentsOfFile:filePathWithName];
I modify your code :
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString*patientlastName;
NSString*test=#"nicej";
patientlastName=[test stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString * filePath = [NSString stringWithFormat:#"%#/Documents//%#.png", NSHomeDirectory(),patientlastName];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
if (fileExists == 0)
{
NSLog(#"File is Not exists");
appDelegate.imagePath=#"";
}
else
{
appDelegate.imagePath=filePath;
}
PictureDisplayViewController *targetController = [[PictureDisplayViewController alloc] initWithNibName:#"PictureDisplayViewController" bundle:nil];
targetController.modalPresentationStyle = UIModalPresentationFullScreen;
[self.splitViewController presentViewController:targetController animated:YES completion:nil];
In PictureDisplayViewController
if(appDelegate.imagePath)
{
UIImage *image=[UIImage imageWithContentsOfFile:appDelegate.imagePath];
}
else
{
imageView.image=[UIImage imageNamed:#"default.png"];
}
default.png is a default png pic in your bundle resource

Retrieve multiple images from document directory

I have folder named "2" in document directory. Now in folder "2", I have five images named 0.png, 1.png, 2.png, 3.png and 4.png. I want to retrieve these images and save into an array. I have code but it returns only one image.
int b=2;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSLog(#"%#",paths);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%d/0.png",b]];
UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];
NSLog(#"%#",getImagePath);
imge.image=img;
Just use NSFileManager, something like this:
[[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error];
It will give you an array with all the file paths for all files in the given path.
This is how you load the images from the array of URLs.
NSArray *paths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error];
for (NSURL *url in paths)
{
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
}
You should loop through the images.
int b=2;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSLog(#"%#",paths);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSMutableArray *images = [NSMutableArray arrayWithCapacity:5];
for (int i = 0; i < 5; i++)
{
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%d/%d.png",b, i]];
UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];
[images addObject:img];
NSLog(#"%#",getImagePath);
imge.image=img;
}
Instead of hardcoding the count and names of the image files, as Dominik suggested you should use contentsOfDirectoryAtPath:error: to get the list of files in that directory.
NSError *error = nil;
NSArray *imageFileNames = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%d",b]] error:&error];

How to retrieve UIImage from particular folder in document directory in iPhone

I have used the below function to store images locally in a folder created in document directory
NSError *error;
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
// FOR STORING IMAGE INTO FOLDER CREATED IN DOCUMENT DIRECTORY
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"/ImagesFolder"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
NSData* imageData = UIImagePNGRepresentation(imageView.image);
NSString* incrementedImgStr = [NSString stringWithFormat:#"Image%d.png",delegate.dirCountImages];
NSString* fullPathToFile2 = [dataPath stringByAppendingPathComponent:incrementedImgStr];
[imageData writeToFile:fullPathToFile2 atomically:NO];
However how do i retrieve images from that particular folder in document directory
-(NSMutableArray*)getPhotoFileNames:(NSString*)parentFolderName
{
NSString *path = [NSString stringWithFormat:#"%#/%#",[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)objectAtIndex:0], parentFolderName];
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
NSEnumerator *enumerator = [dirContents objectEnumerator];
id fileName;
NSMutableArray *fileArray = [[NSMutableArray alloc] init];
while (fileName = [enumerator nextObject])
{
NSString *fullFilePath = [path stringByAppendingPathComponent:fileName];
NSRange textRangeJpg = [[fileName lowercaseString] rangeOfString:[#".png" lowercaseString]];
if (textRangeJpg.location != NSNotFound)
{
UIImage *originalImage = [UIImage imageWithContentsOfFile:fullFilePath];
[fileArray addObject:originalImage];
}
}
return fileArray;
}
Just need to have the full path and use this method. [[UIImage alloc]initWithContentsOfFile: <#path#>] remember to release the image after if you are not using ARC.
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100,100,100,100)];
imageView.image = [[UIImage alloc] initWithData:myImage];
[myView addSubview:imageView];
or see that link http://www.iphonedevsdk.com/forum/iphone-sdk-development/23840-placing-image-url-image-view.html
Perhaps this will help you.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:#"Images"];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:#"Image.png"];
[cell.songImageView setImage:[UIImage imageWithContentsOfFile:documentsDirectory]];

How to store image in big size in local?

I am storing an image locally but my image is store in small size. i want to store this image in orignal size how can i do this ?
my code for store image is like this ..
NSString *myurl = [[NSString alloc]init];
myurl =[data valueForKey:#"large_image"];
NSData *mydata = [[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:myurl]] retain];
UIImage *myimage = [[[UIImage alloc] initWithData:mydata] retain];
NSData *imageData = UIImagePNGRepresentation(myimage);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// NSString *imageName = [NSString stringWithFormat:#"image.png",documentsDirectory];
NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:Imagename];
[imageData writeToFile:fullPathToFile atomically:YES];
this is stored image
and this is orignal image
i am doing wrong in code. this is the reason. why this happend.
may be it is because of leak ok knowledge that time. :)
other wise all things is correct only.

store file in NSDocumentDirectory of iPhone

I am using a UIImagePickerController in my application.
After selecting an image by user,
image should be saved at application Documents Directory,
my Code is Give Below.
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
[picker dismissModalViewControllerAnimated:YES];
NSData *imgData = UIImagePNGRepresentation([info objectForKey:#"UIImagePickerControllerOriginalImage"]);
UIImage *img = [[UIImage alloc] initWithData:imgData];
stuImgView.image = img;
[img release];
//write image
NSString *imageFilename = [NSString stringWithFormat:#"%i.jpg", currentStudent.stuNo];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [NSString stringWithFormat:#"%#/%#", [paths objectAtIndex:0], imageFilename];
UIImage *stuImg;
BOOL success;
NSFileManager *fm=[NSFileManager defaultManager];
// how to store file at documents dir????
}
i dont know how to use file manager to store a file?
Help me Out.
Thanks in advance.
You can use writeToFile:atomically::
[imgData writeToFile:path atomically:NO];