简体   繁体   中英

How can i display images from document directory to image view?

I want to display my images from document directory... i got the contents of directory. but when i want to display image, it display nothing... I checked in document directory.. there are images.. but don't know why it is not displaying in UIimage view???

can anybody help me??? where is the problem???

MY code

NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:newDir  error:nil];
setImage.image = [UIImage imageWithContentsOfFile:[filePathsArray objectAtIndex:0]];

Hey i got the answer..... Please follow this link

here

Thanx to everyone for their precious help........

I think you are confusing UIImage with UIImageView . After creating your UIImage setImage, you need to set it to a UIImageView:

UIImageView *myImageView = [[UIImageView alloc] initWithImage:setImage];

then you need to add the myImageView to your view:

[self.view addSubview:myImageView];

EDIT :

to check for if the file exists at a path you use fileExistsAtPath and log the output:

if ([[NSFileManager defaultManager] fileExistsAtPath:myPath]) {

     NSLog(@"file exists!");
}

myPath would be the NSString for the path you are obtaining. I think most likely that you are not getting the path to the file correctly. Are you sure the images are in the documents directory? if you are using the simulator you can always use finder to locate the files and folders inside your app's sandbox. try to look your images up manually by going to

 ~/Library/Application Support/iPhone Simulator/x.x/Applications/

hope this helps.

Use this code..

NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );

NSString *docDirectory = [sysPaths objectAtIndex:0];

NSString *imagePath=[docDirectory stringByAppendingPathComponent:@"your image-name"];

UIImageView *myimage=[UIImageView alloc] initWithFrame:CGRectMake(0,0,20,20)];
myimage.image=[UIImage imageWithData:[NSData dataWithContentsOfFile:imagePath]];    
[self.view addSubView:myimage];
[myimage release];

may this will help you...

If your image in the document directory use following:

NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *newPath = [directory stringByAppendingPathComponent:@"myMovie.m3u8"];
    UIImage *image=[UIImage imageWithContentsOfFile:newPath];

You have used path for document directory but not provided path for image stored there.. check following here

NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:newDir  error:nil];
setImage.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/imageName.png", [filePathsArray objectAtIndex:0]]];

Try this,

NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:newDir  error:nil];
NSString *str_imgpath = [NSString stringWithFormat:@"%@/imageName.png", [filePathsArray objectAtIndex:0]]
imgView.image = [UIImage imageWithData:[NSData dataWithContentsOfFile:str_imgpath]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM