How to display all images from NSDocument directory

风流意气都作罢 提交于 2019-11-30 07:32:40

This answer is about how to retrieve images from documents directory and displaying them to UITableView.....

first of all you have to get all images from your documents directory to an array....

-(void)viewWillAppear:(BOOL)animated
{

    arrayOfImages = [[NSMutableArray alloc]init];
NSError *error = nil;


    NSString *stringPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];

    NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath: stringPath  error:&error];

  for(int i=0;i<[filePathsArray count];i++)
  {
      NSString *strFilePath = [filePathsArray objectAtIndex:i];
      if ([[strFilePath pathExtension] isEqualToString:@"jpg"] || [[strFilePath pathExtension] isEqualToString:@"png"] || [[strFilePath pathExtension] isEqualToString:@"PNG"]) 
      {
         NSString *imagePath = [[stringPath stringByAppendingString:@"/"] stringByAppendingString:strFilePath];
         NSData *data = [NSData dataWithContentsOfFile:imagePath];
        if(data)
        {
          UIImage *image = [UIImage imageWithData:data];
          [arrayOfImages addObject:image];
        }
      }

  }

}

after that - using this array you can show the image in uitableview cell remember dont add the table view on your view untill your array is fill.....

#pragma mark - UItableViewDelegate methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [arrayOfImages count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tablView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    //adjust your imageview frame according to you
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0.0, 0.0, 470.0, 80.0)];

    [imageView setImage:[arrayOfImages objectAtIndex:indexPath.row]];
    [cell.contentView addSubview:imageView];
    return cell;

}

Now run it will work ..

You can directly get contents by this :

NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] error:NULL];

To set Image :

[imgView setImage:[UIImage imageWithContentsOfFile:"Your complete path"]];

You can get all the image files from the Document Directory this way:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];
    NSMutableArray *imgFiles = [[NSMutableArray alloc] init];
    for (int i=0; i<filePathsArray.count; i++) {
        NSString *strFilePath = [filePathsArray objectAtIndex:0];
        if ([[strFilePath pathExtension] isEqualToString:@"jpg"]) {
            [imgFiles addObject:[filePathsArray objectAtIndex:i]];
        }
    }

    NSLog(@"array with paths of image files in the Document Directory %@", filePathsArray);

And then you can display the images into the UITableView like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 50, 50)];
    img.image = [UIImage imageWithContentsOfFile:[imgFiles objectAtIndex:indexPath.row]];
    [cell addSubview:img];

    return cell;
}

Cheers!!!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!