11

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 ?

Ankit Chauhan
  • 2,375
  • 6
  • 25
  • 37

7 Answers7

18

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

Landei
  • 54,104
  • 13
  • 100
  • 195
Dinakar
  • 1,198
  • 15
  • 37
  • Your sample code is for the bundle images. While here we need to get the image from document directory to the image view. – AppAspect Jul 12 '11 at 11:41
7

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]];
rptwsthi
  • 10,094
  • 10
  • 68
  • 109
6

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.

AppAspect
  • 4,461
  • 2
  • 30
  • 46
  • You can add the pathe of the imageview and the you can directly add the image to the imageview from that path using this: objImageView.image = [UIImage imageWithContentsOfFile: imageFilePath]; – AppAspect Jul 12 '11 at 11:49
4
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

Praveen S
  • 10,355
  • 2
  • 43
  • 69
TalaT
  • 943
  • 2
  • 12
  • 20
1

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.

Pankaj purohit
  • 485
  • 3
  • 10
0

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

}
Joe Barbour
  • 842
  • 9
  • 14
0

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];
Dixit Akabari
  • 2,419
  • 13
  • 26